iOS UILabel 文字底部加阴影效果

2022-12-14  本文已影响0人  YannChee
1.文字底部加阴影

我们可以给UILabel加一个分类扩充方法


@objc
extension UILabel {
    
    // 文字底部加阴影
    @objc
    func qy_bottomShadow(){
        
        guard let text = text else {
            return
        }
        
        let attrString = NSMutableAttributedString(string: text)
        
        let shadow = NSShadow()
        shadow.shadowColor = UIColor(red:0.47,green:0.41,blue:0.65,alpha:1)
        shadow.shadowBlurRadius = 1
        shadow.shadowOffset = CGSize(width: 0, height: 2)
        
        let attribute: [NSAttributedString.Key : Any] = [.shadow: shadow]
        
        attrString.addAttributes(attribute,
                                 range: NSRange(location: 0, length: attrString.length))
        
        attributedText = attrString
    }
  
}

2.文字底部加阴影 + 文字描边

@objc
extension UILabel {
    
    // 文字底部加阴影 + 文字描边
    @objc
    func qy_bottomShadowAndStroke() {
        guard let text = text else {
            return
        }
        
        let attrString = NSMutableAttributedString(string: text)
    
        let shadow = NSShadow()
        shadow.shadowColor = UIColor(red:0.47,green:0.41,blue:0.65,alpha:1)
        shadow.shadowBlurRadius = 1
        shadow.shadowOffset = CGSize(width: 0, height: 2)

       
        let attribute: [NSAttributedString.Key : Any] = [
            .shadow: shadow,
            .strokeColor:UIColor(red: 0.7,green: 0.63,blue: 0.95,alpha: 1),
            .strokeWidth: -6
        ]
        
        attrString.addAttributes(attribute, range: NSRange(location: 0, length: attrString.length))
        
        attributedText = attrString
    }
}
上一篇下一篇

猜你喜欢

热点阅读