我想给我的标签添加圆角,也想给标签添加阴影,但是我的阴影方法和圆角半径方法不能在标签上一起使用。
这是我用来制作标签的代码
extension UILabel
{
func ShadowLabel() {
self.layer.shadowColor = UIColor.lightGray.cgColor
self.layer.shadowRadius = 2
self.layer.shadowOpacity = 1
self.layer.shadowOffset = CGSize(width: 5, height: 5)
}
}
extension UILabel
{
func RoundCornerLabel() {
self.layer.cornerRadius = frame.size.height/2
self.layer.masksToBounds = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
label.shadowLabel()
label.RoundCornerLabel()
}
最佳答案
你想做的事是不可能的。阴影应用于 UIView
的边界之外,但是 cornerRadius
不屏蔽 bounds
将不可见的 UIView
.所以,你将不得不使用一个容器 UIView
.
如果您想以通用方式编写此代码,请编写 UIView
返回 UIView
的扩展包含您要为其应用阴影的 View
关于swift - 如何在不使用 UIView 的情况下快速向标签添加阴影和圆角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55847065/