Attributed
用于属性字符串的µ框架。
什么是Attributed?
Attributed旨在替代当前版本的NSAttributedString API。
NSAttributedString接口有几个缺点。如果您不知道设置特定属性所需的键和类型,您必须花费时间检查文档。另一个问题是安全性:将类型为[String: Any]的字典传递给NSAttributedString构造函数是潜在的运行时崩溃,只是等待触发。
通过扩展当前NSAttributedString接口,提供易于使用、强类型的流畅API,Attributed为开发者提供了更好的替代方案。
功能
- 使用强类型、简单和流畅的接口创建
NSAttributedString实例 - 使用
+组合NSAttributedString - 通过提供
Range,部分应用属性到NSAttributedString的某部分
您没看到您需要的功能?
请随意打开一个问题请求您想要的功能,或者发送一个拉取请求!
用法
通过闭包组合创建新的 NSAttributedString
"This is not a string".at.attributed {
return $0.foreground(color: .red)
.font(UIFont(name: "Chalkduster", size: 24.0)!)
.underlineStyle(.styleSingle)
}通过传递一个属性对象创建新的 NSAttributedString
首先创建一个 Attributes 对象
let attributes = Attributes {
return $0.foreground(color: .red)
.font(UIFont(name: "Chalkduster", size: 24.0)!)
.underlineStyle(.styleSingle)
}然后将 Attributes 应用到 String 上
"Hello".at.attributed(with: attributes)使用 + 符号合并 NSAttributedString
此库定义了一个用于连接 NSAttributedString 的连接操作符 +。 + 与 String 的行为相同,与 NSAttributedString 一起使用。这可以用于将具有不同属性的 NSAttributedStrings 混合并产生所需效果,而无需指定应用不同属性的范本。
let bodyAttributes = Attributes {
return $0.foreground(color: .purple)
.font(UIFont(name: "Noteworthy-Light", size: 20.0)!)
}
let authorAttributes = bodyAttributes.foreground(color: .black)
"I think theres something strangely musical about noise.".at.attributed(with: bodyAttributes)
+ "\n - Trent Reznor".at.attributed(with: authorAttributes)安装
Carthage
如果你使用Carthage来管理你的依赖项,只需将Attributed添加到你的Cartfile
github "Nirma/Attributed"
如果你使用Carthage来构建你的依赖项,请确保你已将Attributed.framework添加到你的目标“链接框架和库”部分,并在Carthage框架复制构建阶段中包含了Attributed.framework。
CocoaPods
如果你使用CocoaPods来管理你的依赖项,只需将Attributed添加到你的Podfile
pod 'AttributedLib'Requirements
- Xcode 9.0
- Swift 4.0+
Contribution
欢迎提供贡献!
License
Attributed是免费软件,可以依据LICENSE文件中指定的条款进行重新分发。