一直以来,在 Cocoa 体系里构建富文本是一件很繁琐的事情,使用 Foundation 框架里的 NSAttributeString
来进行构建,有着要区分可变与不可变,传入须要查阅文档的参数字典,没有类型检查等种种别扭之处。Swift 5 出现的新字符串插值特性,让处理自定义字符串字面量成为可能。git
简单来讲,这个特性能让开发者本身实现对字面量中插值的解析和逻辑实现。例如:github
let string:CustomString = "\(num: 10)"
复制代码
这样的字符串字面量,开发者能够经过实现相似 func appendInterpolation(num: Int)
这样的方法,实现对 num
的处理。要实现这样的效果,大概分为如下几个步骤:express
ExpressibleByStringInterpolation
StringInterpolationProtocol
的插值类型func appenInterpolation(...)
重载方法,并处理对应参数的逻辑这样经过字符串插值,理论上能够实现任意的功能,好比经过字符串构建成 SQL 操做,正则操做,生成 HTML 网页,甚至感受能够实现一些运行时的黑科技。swift
首先自定义类型,并先实现 ExpressibleByStringLiteral
用于支持字符串字面量(ExpressibleByStringLiteral
是 ExpressibleByStringLiteral
的子协议)。这样咱们就能够经过字符串字面量构建一个没有任何属性的 NSAttributedString
。安全
public struct EasyText {
public let attributedString: NSAttributedString
}
extension EasyText: ExpressibleByStringLiteral {
public init(stringLiteral: String) {
attributedString = NSAttributedString(string: stringLiteral)
}
}
复制代码
接着实现 ExpressibleByStringInterpolation
协议,嵌套插值类型,并遵循 StringInterpolationProtocol
。该插值构建完会经过 init(stringInterpolation: StringInterpolation)
方法来实现构建自定义类型。appendLiteral
方法用于正常的字符串类型的插值处理。app
extension EasyText: ExpressibleByStringInterpolation {
public init(stringInterpolation: StringInterpolation) {
attributedString = NSAttributedString(attributedString: stringInterpolation.rawAttributedString)
}
public struct StringInterpolation:StringInterpolationProtocol {
let rawAttributedString:NSMutableAttributedString
public init(literalCapacity: Int, interpolationCount: Int) {
rawAttributedString = NSMutableAttributedString()
}
public func appendLiteral(_ literal:String) {
rawAttributedString.append(NSAttributedString(string: literal))
}
}
}
复制代码
接下来实现一到多个 appendInterpolation
方法,例如让插值支持添加 NSTextAttachment
:框架
public struct StringInterpolation:StringInterpolationProtocol {
// ...
public func appendInterpolation(attachment:NSTextAttachment) {
rawAttributedString.append(NSAttributedString(attachment: attachment))
}
}
复制代码
笔者将 NSAttributedString
里的属性都使用字符串插值进行了支持,实现了一个几百行的库。使用这个库,能够作到如下面简单的方式构建 NSAttributedString
:性能
let text:EasyText = "\("font is 30 pt and color is yellow", .font(.systemFont(ofSize: 20)), .color(.blue))"
textView.attributedText = text.attributedString
复制代码
有如下特性:spa
NSAttributedString.Key
均有类型检查NSAttributedString.Key
,暂时没有支持的提供接口构建+
、+=
,以及与其余类型 String
、NSAttributedString
的运算更多使用方法详见 例子。code
EasyText 已开源,能够经过 Cocoapods、Cathrage、SwiftPackageManager 进行集成。
若是您以为有帮助,不妨给个 star 鼓励一下,有任何问题欢迎评论。
StringInterpolation in Swift 5 — Introduction