CuteAttribute
Swift 中处理富文本字符串的一种优雅方式。使用 .cute
可以方便地创建富文本字符串,无论从 String
、NSString
、NSAttributedString
或 NSMutableAttributedString
。它非常神奇,几乎所有方法都支持 链式调用
。之所以创建这个项目,是因为几天前遇到了 Typeset。但发现 Swift 使用它非常困难。所以我就做了这个。
支持
Swift 3.0 & iOS 8+
安装
使用CocoaPods
- 将其添加到Podfile文件中。
pod 'CuteAttribute'
- 然后运行以下命令。
pod install
- 在项目中导入头文件。
import CuteAttribute
使用Carthage
- 将CuteAttribute添加到Cartfile中。
github "qiuncheng/CuteAttribute"
- 运行以下命令
carthage update --platform ios
-
按照标准Carthage安装指令中的步骤,将CuteAttribute添加到项目中。
-
在项目中导入头文件。
import CuteAttribute
手动安装
- 下载完整文件。
- 将CuteAttribute文件夹拖到您的项目中。
用法
比较
属性名称 | 结果 | NSAttributedString | CuteAttribute |
---|---|---|---|
颜色 | ![]() |
NSMutableAttributedString(string: "hello world", attributes: [.foregroundColor: UIColor.red]) | "hello world".cute.matchAll().color(.red) |
基线偏移量 | ![]() |
NSMutableAttributedString(string: "hello world", attributes: [.baselineOffset: 10]) | "hello world".cute.matchAll().baseline(10) |
下划线 | ![]() |
NSMutableAttributedString(string: "hello world", attributes: [.underlineColor: UIColor.red, .underlineStyle: NSUnderlineStyle.styleSingle.rawValue]) | "hello world".cute.matchAll().underline(.styleSingle).underlineColor(.red) |
字体 | ![]() |
NSMutableAttributedString(string: "hello world", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20)]) | "hello world".cute.matchAll().font(UIFont.systemFont(ofSize: 20)) |
删除线 | ![]() |
NSMutableAttributedString(string: "hello world", attributes: [.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue, .strikethroughColor: UIColor.red]) | "hello world".cute.matchAll().strikeThrough(.styleSingle).strokeColor(.red) |
链接 | ![]() |
NSMutableAttributedString(string: "hello world", attributes: [.link: "https://blog.vsccw.com"]) | "hello world".cute.matchAll().link("https://blog.vsccw.com") |
连字符 | ![]() |
NSMutableAttributedString(string: "hello world", attributes: [.ligature: 1]) | "hello world".cute.matchAll().ligature(1) |
字距调整 | ![]() |
NSMutableAttributedString(string: "hello world", attributes: [.kern: 10]) | "hello world".cute.matchAll().kern(10) |
描边 | ![]() |
NSMutableAttributedString(string: "hello world", attributes: [.strokeColor: UIColor.red, .strokeWidth: 10]) | "hello world".cute.matchAll().strokeColor(.red).strokeWidth(10) |
阴影 | let shadow = NSShadow(); shadow.shadowColor = UIColor.red; shadow.shadowOffset = CGSize(width: 4, height: 4); shadow.shadowBlurRadius = 10; ![]() |
NSMutableAttributedString(string: "hello world", attributes: [.shadow: shadow]) | "hello world".cute.matchAll().shadow(shadow) |
文字特效 | ![]() |
"hello world".cute.matchAll().textEffect("NSTextEffectLetterpressStyle") | "hello world".cute.matchAll().textEffect("NSTextEffectLetterpressStyle") |
倾斜度 | ![]() |
NSMutableAttributedString(string: "hello world", attributes: [.obliqueness: 10]) | "hello world".cute.matchAll().obliqueness(10) |
膨胀 | ![]() |
NSMutableAttributedString(string: "hello world", attributes: [.expansion: 10]) | "hello world".cute.matchAll().expansion(10) |
文字附加 | let attachment = NSTextAttachment(); attachment.image = UIImage(named: "hello.png"); ![]() |
NSMutableAttributedString(string: "hello world", attributes: [.attachment: attachment]) | "hello world".cute.matchAll().textAttachment(attachment) |
待办事项
- 已记录。
- 示例。
- 测试。
- 更多便利。
感谢
排版 : 高效地处理 Objective-C
中的 AttributedString。
许可
在 MIT 许可下