我试图给标签文字留一个阴影但是没有显示。
我的代码
private func drawValueLabel() {
valueLabel.layer.shadowColor = UIColor.red.cgColor
valueLabel.layer.shadowOffset = CGSize.init(width: 15.0, height: 15.0)
valueLabel.layer.shadowRadius = 3.0
valueLabel.layer.shadowOpacity = 1
valueLabel.layer.masksToBounds = false
valueLabel.clipsToBounds = false
valueLabel.layer.shouldRasterize = true
valueLabel.drawText(in: self.bounds)
}
帮我展示阴影
感谢
答案 0 :(得分:2)
我正在使用此UIView扩展来添加阴影。使用此扩展程序,您可以从故事板添加阴影。
extension UIView {
@IBInspectable var shadow: Bool {
get {
return layer.shadowOpacity > 0.0
}
set {
if newValue == true {
self.addShadow()
}
}
}
func addShadow(shadowColor: CGColor = UIColor.black.cgColor,
shadowOffset: CGSize = CGSize(width: 1.0, height: 2.0),
shadowOpacity: Float = 0.4,
shadowRadius: CGFloat = 3.0) {
layer.shadowColor = shadowColor
layer.shadowOffset = shadowOffset
layer.shadowOpacity = shadowOpacity
layer.shadowRadius = shadowRadius
}
}
答案 1 :(得分:0)
您的代码对我来说很好。尝试给出一些较小的偏移量,以便您能够看到它
label.layer.shadowOffset = CGSize.init(width: 3.0, height: 3.0)
答案 2 :(得分:0)
使用此 -
label.layer.shadowColor = UIColor.black.cgColor
label.layer.shadowOpacity = 0.5
label.layer.shadowRadius = 2.0
label.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)