美文网首页
iOS UILabel 文字底部加阴影效果

iOS UILabel 文字底部加阴影效果

作者: YannChee | 来源:发表于2022-12-14 00:52 被阅读0次
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
    }
}

相关文章

网友评论

      本文标题:iOS UILabel 文字底部加阴影效果

      本文链接:https://www.haomeiwen.com/subject/qixhqdtx.html