PimpMyString 是围绕 NSAttributedString 的一种 iOS/OSX 包装库。它的优点是具有强类型,而 NSStringAdditions 不是。
PimpMyString.xcodeproj
添加到您的 workspace 并将适当的框架(iOS/OSX)作为项目依赖项添加。只需在源文件中导入 PMS,并调用您需要的 StringTuner。一段示例代码胜过千言万语(取自示例 playgrounds)
import PMS
let string = "Hello PMS !"
let stringTuner = pms_underline(UIColor.redColor(), .StyleDouble)
>>> pms_fgColor(UIColor.purpleColor())
>>> pms_bgColor(UIColor.blackColor())
>>> pms_font(UIFont.boldSystemFontOfSize(24))
>>> pms_fgColor(UIColor.orangeColor(), range: NSMakeRange(0, 1))
>>> pms_strikeThrough(UIColor.greenColor().colorWithAlphaComponent(0.5), NSUnderlineStyle.StyleSingle)
>>> pms_skew(0.3)
>>> pms_stroke(UIColor.brownColor(), 4)
>>> pms_effectLetterPress()
let attributedString = stringTuner(NSAttributedString(string: string))
// Do whatever you want with the attributed string.
label.attributedText = attributedString
所有属性将从左到右进行处理。
初始版本
PimpMyString 在新 BSD 许可证下发布。有关详细信息,请参阅 LICENSE。