2017-08-01 20 views
0

我试图给标签文本添加阴影,但不显示。标签阴影不能在swift中工作

我的代码

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

你的代码是好的,正确的 –

+0

https://stackoverflow.com/questions/44367129/uitableviewcell-shadows/44367371#44367371 –

回答

0

你的代码是为我工作的罚款。尝试给一些较小的偏移,以便您能够看到它像

label.layer.shadowOffset = CGSize.init(width: 3.0, height: 3.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 
    } 
} 
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)