SwiftyAttributes
一个针对富文本字符串的 Swifty API。
使用 SwiftyAttributes,您可以创建以下这样的富文本字符串
let fancyString = "Hello World!".withTextColor(.blue).withUnderlineStyle(.styleSingle)
或者,使用 Attribute
枚举
let fancyString = "Hello World!".withAttributes([
.backgroundColor(.magenta),
.strokeColor(.orange),
.strokeWidth(1),
.baselineOffset(5.2)
])
您还可以使用加号轻松合并富文本字符串
let fancyString = "Hello".withFont(.systemFont(ofSize: 12)) + " World!".withFont(.systemFont(ofSize: 18))
SwiftyAttributes 支持 Cocoa 和 Cocoa Touch 中可用的所有属性。
要求
- iOS 8.0+, macOS 10.11+, watchOS 2.0+, tvOS 9.0+
- Swift 4.2+
- Xcode 10.0+
安装
使用 CocoaPods
pod 'SwiftyAttributes'
使用 Carthage
github "eddiekaiger/SwiftyAttributes"
使用方法
在 SwiftyAttributes
中初始化属性字符串有几种方式
-
使用
with[Attribute]
扩展"Hello World".withUnderlineColor(.red).withUnderlineStyle(.styleDouble)
-
使用
Attribute
枚举扩展"Hello World".withAttributes([.underlineColor(.red), .underlineStyle(.styleDouble)])
-
使用初始化器中的
Attribute
枚举NSAttributedString(string: "Hello World", swiftyAttributes: [.kern(5), .backgroundColor(.gray)])
您可以使用内置的 NSAttributedString.Key
枚举在特定位置检索属性
let attr: Attribute? = myAttributedString.swiftyAttribute(.shadow, at: 5)
提供了一些 API 方法来使用这些新枚举以及 Swift 的 Range
类型,而不是 NSRange
。一些方法签名包括
extension NSMutableAttributedString {
func addAttributes(_ attributes: [Attribute], range: Range<Int>)
func addAttributes(_ attributes: [Attribute], range: NSRange)
func setAttributes(_ attributes: [Attribute], range: Range<Int>)
func setAttributes(_ attributes: [Attribute], range: NSRange)
func replaceCharacters(in range: Range<Int>, with str: String)
func replaceCharacters(in range: Range<Int>, with attrString: NSAttributedString)
func deleteCharacters(in range: Range<Int>)
func removeAttribute(_ name: NSAttributedStringKey, range: Range<Int>)
}
extension NSAttributedString {
convenience init(string str: String, swiftyAttributes: [Attribute])
func withAttributes(_ attributes: [Attribute]) -> NSMutableAttributedString
func withAttribute(_ attribute: Attribute) -> NSMutableAttributedString
func attributedSubstring(from range: Range<Int>) -> NSAttributedString
func swiftyAttribute(_ attrName: NSAttributedStringKey, at location: Int, effectiveRange range: NSRangePointer? = nil) -> Attribute?
func swiftyAttributes(in range: Range<Int>, options: NSAttributedString.EnumerationOptions = []) -> [([Attribute], Range<Int>)]
func enumerateSwiftyAttributes(in enumerationRange: Range<Int>, options: NSAttributedString.EnumerationOptions = [], using block: (_ attrs: [Attribute], _ range: Range<Int>, _ stop: UnsafeMutablePointer<ObjCBool>) -> Void)
func enumerateSwiftyAttribute(_ attrName: NSAttributedStringKey, in enumerationRange: Range<Int>, options: NSAttributedString.EnumerationOptions = [], using block: (_ value: Any?, _ range: Range<Int>, _ stop: UnsafeMutablePointer<ObjCBool>) -> Void)
}
extension String {
var attributedString: NSMutableAttributedString
func withAttributes(_ attributes: [Attribute]) -> NSMutableAttributedString
func withAttribute(_ attribute: Attribute) -> NSMutableAttributedString
}
// ... and more!
支持
如有问题、支持或建议,请创建一个 issue。
版权
SwiftyAttributes 使用 MIT 许可。有关更多信息,请参阅 LICENSE 文件。