SwiftyAttributes
一个用于属性字符串的Swift风格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+
安装
Swift包管理器
dependencies: [
.package(url: "https://github.com/eddiekaiger/SwiftyAttributes.git", from: "5.3.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
}
// ... 还有更多!
支持
如有问题、支持请求和建议,请开启一个issue。
许可
SwiftyAttributes基于MIT许可证可用。更多信息请参阅LICENSE文件。