AttributedString

public struct AttributedString
extension AttributedString: ExpressibleByStringLiteral
extension AttributedString: CustomStringConvertible
extension AttributedString: ExpressibleByStringInterpolation

富文本字符串插值

使用

let attributedString: AttributedString = """
\("你好,世界", .color(.red), .font(UIFont.systemFont(ofSize: 20)), .bgColor(.green))\n
\("百度URL", .link("https://www.baidu.com"), .color(.red), .font(UIFont.systemFont(ofSize: 20)))\n
"""
// 修改URL颜色
// 要打开URL, 需要实现UITextView代理方法shouldInteractWith
textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.brown]
textView.attributedText = attributedString.attributedString