使用 Swift 5 字符串插值快速构建 AttributeString

1,434 阅读2分钟

一直以来,在 Cocoa 体系里构建富文本是一件很繁琐的事情,使用 Foundation 框架里的 NSAttributeString 来进行构建,有着要区分可变与不可变,传入需要查阅文档的参数字典,没有类型检查等种种别扭之处。Swift 5 出现的新字符串插值特性,让处理自定义字符串字面量成为可能。

Swift 5 的字符串插值

简单来说,这个特性能让开发者自己实现对字面量中插值的解析和逻辑实现。例如:

let string:CustomString = "\(num: 10)"

这样的字符串字面量,开发者可以通过实现类似 func appendInterpolation(num: Int) 这样的方法,实现对 num 的处理。要实现这样的效果,大概分为以下几个步骤:

  1. 自定义类型并遵循 ExpressibleByStringInterpolation
  2. 在类型中嵌套遵循 StringInterpolationProtocol 的插值类型
  3. 插值类型自定义一到多个不同参数的 func appenInterpolation(...) 重载方法,并处理对应参数的逻辑

这样通过字符串插值,理论上可以实现任意的功能,比如通过字符串构建成 SQL 操作,正则操作,生成 HTML 网页,甚至感觉可以实现一些运行时的黑科技。

实现 AttributeString 处理

首先自定义类型,并先实现 ExpressibleByStringLiteral 用于支持字符串字面量(ExpressibleByStringLiteralExpressibleByStringLiteral 的子协议)。这样我们就可以通过字符串字面量构建一个没有任何属性的 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 方法用于正常的字符串类型的插值处理。

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))
    }
}

EasyText 使用

笔者将 NSAttributedString 里的属性都使用字符串插值进行了支持,实现了一个几百行的库。使用这个库,可以做到以下面简单的方式构建 NSAttributedString

let text:EasyText = "\("font is 30 pt and color is yellow", .font(.systemFont(ofSize: 20)), .color(.blue))"

textView.attributedText = text.attributedString

有以下特性:

  • 类型安全,所有支持的 NSAttributedString.Key 均有类型检查
  • 支持多平台,iOS/macOS
  • 支持大部分常用 NSAttributedString.Key,暂时没有支持的提供接口构建
  • 支持常用的运算符,如 ++=,以及与其他类型 StringNSAttributedString 的运算
  • 支持插入图片

更多使用方法详见 例子

最后

EasyText 已开源,可以通过 Cocoapods、Cathrage、SwiftPackageManager 进行集成。

如果您觉得有帮助,不妨给个 star 鼓励一下,有任何问题欢迎评论。

参考链接

StringInterpolation in Swift 5 — Introduction

StringInterpolation in Swift 5 — AttributedStrings

Fix ExpressibleByStringInterpolation