RichString
Swift 中处理富文本字符串的最简单方式
简介
这个 Swift 框架被构建为简化处理 NSAttributedString
。它通过添加额外的类型来实现这一点;它只是扩展了您已经拥有的现有类型,确保它与任何其他使用 NSAttributedString
的方法完全兼容。
该框架做出的核心假设是,您首先通过适当配置所有组件来设置您的富文本字符串,然后将它们连接起来。
它允许您执行类似以下操作
let title = "This is a title".fontSize(17).color(.blue)
let text = "This is some text".paragraphStyle {
$0.firstLineHeadIndent = 20
}
let content = title + text
概述
该框架提供了以下原语。有单元测试;请查看它们以获取更多示例。
+
用于连接属性字符串
运算符 给定两个字符串,您可以使用 +
运算符将一个字符串连接到另一个字符串上。
let s1 = NSAttributedString()
let s2 = NSAttributedString()
let concatenated = s1 + s2
RichString
协议针对 String
、NSString
和 NSAttributedString
font(_:)
应用指定的字体。类型 Font
是 iOS 上 UIFont
的别名,以及在 macOS 上为 NSFont
。
let attributedString = "text".font(.systemFont(ofSize: 12))
fontSize(_:)
应用指定的字体大小。如果在属性字符串上尚未设置字体,则假定为 Font.systemFont
。
注意:与 iOS 不同,在 macOS 上,这个方法返回一个 可选的 属性字符串。
let attributedString = "text".fontSize(12)
paragraphStyle(configure:)
应用一个段落样式,并使用给定的闭包进行配置。如果在属性字符串中已经有了段落样式属性,则会在该段落样式中调用 configure
闭包;否则,将使用一个新的 NSMutableParagraphStyle
。
let attributedString = "Hello World".paragraphStyle {
$0.firstLineHeadIndent = 10
}
paragraphStyle(_:)
应用给定的段落样式。
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.firstLineHeadIndent = 10
let attributedString = "text".paragraphStyle(paragraphStyle)
color(_:)
应用指定的(前景)颜色。
let attributedString = "text".color(.blue)
backgroundColor(_:)
应用指定的背景颜色。
let attributedString = "text".backgroundColor(.yellow)
ligature(_:)
配置是否使用连字符。默认情况下,使用连字符。
let attributedString = "text".ligature(false)
kern(_:)
配置修改默认字距调整的量。默认值 0
表示不应用字距调整更改。
let attributedString = "text".kern(0.1)
strikeThrough(style:)
配置删除线样式。
请注意,根据操作系统和版本,并非所有样式都能实际工作。
let attributedString = "text".strikeThrough(style: .styleSingle)
strikeThrough(color:)
配置删除线颜色。仅设置颜色没有效果,样式也必须配置。
let attributedString = "text".strikeThrough(color: .red)
strikeThrough(color:, style:)
配置删除线的颜色和样式。请注意,根据操作系统和版本,并非所有样式都可能生效。
let attributedString = "text".strikeThrough(color: .red, style: .styleDouble)
underline(style:)
配置下划线样式。请注意,根据操作系统和版本,并非所有样式都可能生效。
let attributedString = "text".underline(style: .styleSingle)
underline(color:)
配置下划线颜色。仅设置颜色无效,样式也必须配置。
let attributedString = "text".underline(color: .blue)
underline(color:, style:)
配置下划线的颜色和样式。请注意,根据操作系统和版本,并非所有样式都可能生效。
let attributedString = "text".underline(color: .blue, style: .styleSingle)
stroke(width:, color:)
配置边框。
let attributedString = "text".stroke(width: 2, color: .green)
shadow(configure:)
使用接受 NSShadow
实例的闭包来配置阴影。在 watchOS 上不可用。
let result = "Hello World".shadow {
$0.shadowOffset = CGSize(width: 3, height: 3)
$0.shadowBlurRadius = 2
$0.shadowColor = Color.gray
}
shadow(_:)
通过设置一个 NSShadow
实例来配置阴影。在 watchOS 上不可用。
let shadow = NSShadow()
shadow.shadowColor = .green
shadow.shadowBlurRadius = 2
shadow.shadowOffset = 3
let attributedString = "text".shadow(shadow)
letterPressed()
添加“字母按下”文本效果。
let attributedString = "text".letterPressed()
link(url:)
创建指向给定 URL 的超链接,其中接收器作为文本。
let url = URL(string: "https://example.com")
let attributedString = "text".link(url: url)
link(string:)
创建指向给定 URL 的超链接,其中接收器作为文本。
let urlString = "https://example.com"
let attributedString = "text".link(string: urlString)
attachment(configure:)
创建一个新的 NSTextAttachment
并将其传递给 configure
闭包。在 watchOS 上不可用。
let attributedString = NSAttributedString().attachment {
$0.image = UIImage(...)
$0.bounds = CGRect(...)
}
baselineOffset(_:)
配置基线偏移。
let attributedString = "text".baselineOffset(-0.5)
obliqueness(_:)
配置应应用于字形的角度偏斜。
let attributedString = "text".obliqueness(1.5)
expansion(_:)
配置应应用于字形的扩展。
let attributedString = "text".expansion(2)
NSAttributedString
获取属性值扩展
所有上述方法都有对应的getter来从富文本字符串中检索属性值。它们都返回一个可选项;如果属性没有在富文本字符串中配置,则返回nil
。
这些getter包括
attachment: NSTextAttachment?
(在watchOS上不可用)backgroundColor: Color?
baselineOffset: Float?
color: Color?
expansion: Float?
fontSize: CGFloat?
isLetterPressed: Bool?
kern: Float?
ligature: Bool?
link: NSURL?
obliqueness: Float?
paragraphStyle: NSParagraphStyle?
shadow: NSShadow?
(在watchOS上不可用)strikeThroughColor: Color?
strikeThroughStyle: NSUnderlineStyle?
strokeColor: Color?
strokeWidth: Float?
underlineColor: Color?
underlineStyle: NSUnderlineStyle?
使用方法
您可以使用自己喜欢的任何方式来使用这个库,但两种简单的方法是Carthage和CocoaPods。对于CocoaPods,在医院里输入 pod RichString
。
其他框架
我发现了一些与这个目标相同的其他框架:简化使用 NSAttributedString
。我喜欢我的,主要是因为它有更简单的API,不需要任何额外的类型。但最终选择权在你手中。
《TextAttributes》与该框架类似,但它引入了一种新的类型,您需要设置属性,而不是直接在字符串本身上操作。它没有采用该框架拥有的基于闭包的设置 shadow { ... }
和 paragraphStyle { ... }
的方式。它也不提供该框架所具有的超级方便的 +
操作符。最后,我没有在 tvOS 和 watchOS 上测试这个框架。我非常有信心这些也会一样好,但您需要亲自尝试。当然,欢迎拉取请求。
《TextAttributesUtil》也引入了一种新的类型用于设置属性。此外,它的 API 强制您在源代码中拥有更多层级的嵌套。它只支持 iOS,不支持 macOS。