UILabel的文字排版特效,删除线,下划线等

首先要做文字排版的特效,需要用到 iOS 系统自带的一个 Foundation 框架下的 NSMutableAttributeString .步骤如下:

1.创建属性字串

1
var mAttributeStr:NSMutableAttributedString = NSMutableAttributedString(string: "$¥12345")

2.添加属性

1
2
mAttributeStr.addAttribute(NSStrikethroughStyleAttributeName,   value:NSUnderlineStyle.StyleSingle.rawValue, range:     NSRange(location: 2, length: 5))
mAttributeStr.addAttribute(NSStrikethroughStyleAttributeName, value:NSUnderlineStyle.StyleNone.rawValue, range: NSRange(location: 0, length: 2))

3.赋值给UILabel

1
strikedLabel.attributedText = mAttributeStr

简单3步就创建好了字符串的删除线效果,更多效果请参考我的demo.


Comments