带有层角半径和阴影的 UILabel [英] UILabel with layer Corner radius AND shadow
问题描述
我正在寻找一种方法来显示带有 layer.cornerRadius
和 layer.shadow
的 UILabel
.
i'm looking for a way to display a UILabel
with layer.cornerRadius
and layer.shadow
.
我发现,使用 label.clipsToBounds = true
将设置 cornerRadius
,使用 label.masksToBounds = false
将设置阴影显示
I figured out, that with label.clipsToBounds = true
the cornerRadius
will be set and with label.masksToBounds = false
the shadow will be displayed
只显示阴影,不显示cornerRadius
With both only the shadow, without the cornerRadius will be displayed
let label = UILabel()
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 32, weight: .regular)
label.textColor = .white
label.clipsToBounds = true
label.backgroundColor = Colors.Vibrants.softBlue
label.layer.cornerRadius = 50
label.layer.masksToBounds = false
label.layer.shadowColor = UIColor.black.cgColor
label.layer.shadowOffset = CGSize(width: 5, height: 5)
label.layer.shadowRadius = 5
label.layer.shadowOpacity = 0.7
label.text = "0"
谁能解决这个问题,以便显示cornerRadius
和shadow
?
Can anyone solve this, so that the cornerRadius
AND the shadow
will be displayed?
推荐答案
你为什么不尝试为你的标签添加一个父 UIView
,它会包含背景颜色和cornerRadius.然后将阴影属性保留给标签
Why don't you try adding a parent UIView
for your label that will contain the background color and cornerRadius. Then retain the shadow properties to the label
这篇关于带有层角半径和阴影的 UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!