Swift 风格的优雅的属性字符串组合
SwiftRichString 是一个轻量级的库,它允许在 iOS、macOS、tvOS 以及甚至 watchOS 中轻松地创建和操作属性字符串。它提供了一种方便的方式来存储可以在您的应用程序 UI 元素中重用的样式,允许基于标签的字符串复杂渲染,还包括与 Interface Builder 的集成。
主要功能
特性亮点 | |
---|---|
使用简洁的声明性语法轻松进行样式和字体管理 | |
在文本中附载本地图像(延迟/静态)和远程图像 | |
快速且高度可定制的 XML/HTML 标签字符串渲染 | |
在样式内应用文本转换 | |
原生支持 iOS 11 动态类型 | |
支持 Swift 5.1 的函数构建器以组合字符串 | |
紧凑的代码库,无外部依赖。 |
简单样式
let style = Style {
$0.font = SystemFonts.AmericanTypewriter.font(size: 25) // just pass a string, one of the SystemFonts or an UIFont
$0.color = "#0433FF" // you can use UIColor or HEX string!
$0.underline = (.patternDot, UIColor.red)
$0.alignment = .center
}
let attributedText = "Hello World!".set(style: style) // et voilà!
基于 XML/HTML 标签的渲染
SwiftRichString 允许您通过解析文本的标签来渲染复杂的字符串:每个样式都会通过一个唯一的名称(在标签内使用)进行识别,并且您可以创建一个 StyleXML
(以前是 StyleGroup
),它允许您封装所有样式并按需重复使用(当然,您可以将其注册为全局的)。
// Create your own styles
let normal = Style {
$0.font = SystemFonts.Helvetica_Light.font(size: 15)
}
let bold = Style {
$0.font = SystemFonts.Helvetica_Bold.font(size: 20)
$0.color = UIColor.red
$0.backColor = UIColor.yellow
}
let italic = normal.byAdding {
$0.traitVariants = .italic
}
let myGroup = StyleXML(base: normal, ["bold": bold, "italic": italic])
let str = "Hello <bold>Daniele!</bold>. You're ready to <italic>play with us!</italic>"
self.label?.attributedText = str.set(style: myGroup)
这就是结果!
升级到 3.x
从 4.0 版本开始,SwiftRichString 的 Style
、StyleXML
和 StyleRegEx
都改用 struct 而不是 class。这应该简化并使库在多线程操作中保持稳健。
文档
其他信息
Style
、StyleXML
和 StyleRegEx
简介
SwiftRichString 的主要概念是在 String
和 `NSMutableAttributedString
` 中使用 StyleProtocol
作为可以应用的属性的泛型容器。由 StyleProtocol
派生的具体类包括:Style
、StyleXML
和 StyleRegEx
。
这些类中的每一个都可以作为应用到你字符串、子字符串或富文本字符串的样式的来源。
Style
:将样式应用于字符串或富文本字符串
Style
是一个类,它封装了你可以应用到字符串上的所有属性。目前 AppKit/UIKit 的绝大多数属性都可以通过这个类以类型安全的属性的形式使用。
创建一个 Style
实例非常简单;采用构建者模式的方法,init 类需要回调一个函数,其中 self 实例被传入,这允许你通过保持代码的简洁和可读性来配置你的属性。
let style = Style {
$0.font = SystemFonts.Helvetica_Bold.font(size: 20)
$0.color = UIColor.green
// ... set any other attribute
}
let attrString = "Some text".set(style: style) // attributed string
StyleXML
:为基于标签的复杂字符串应用样式
Style
实例是匿名的;如果你想使用样式实例来渲染一个基于标签的纯字符串,你需要将其包含在 StyleXML
中。你可以把 StyleXML
视为 Styles
的容器(但实际上,由于遵守了共同的 StyleProtocol
协议,你的组也可能包含其他子组)。
let bodyStyle: Style = ...
let h1Style: Style = ...
let h2Style: Style = ...
let group = StyleXML(base: bodyStyle, ["h1": h1Style, "h2": h2Style])
let attrString = "Some <h1>text</h1>, <h2>welcome here</h2>".set(style: group)
以下代码定义了一个组,其中
- 我们定义了一个基础样式。基础样式是应用于整个字符串的样式,可以用作提供要应用于字符串的基础样式的基准。
- 我们还定义了两个其他样式,命名为
h1
和h2
;当解析器遇到由这些标签包围的文本时,这些样式会应用于源字符串。
StyleRegEx
:通过正则表达式应用样式
StyleRegEx
允许你在目标字符串/富文本字符串内部匹配到某些正则表达式时定义一个应用样式。
let emailPattern = "([A-Za-z0-9_\\-\\.\\+])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]+)"
let style = StyleRegEx(pattern: emailPattern) {
$0.color = UIColor.red
$0.backColor = UIColor.yellow
}
let attrString = "My email is [email protected] and my website is http://www.danielemargutti.com".(style: style!)
结果是这个

String & Attributed String Concatenation
SwiftRichString 允许你通过提供 String
、AttributedString
(NSMutableAttributedString
别名)和 Style
之间的自定义 +
运算符来简化字符串连接。
这是一个例子
let body: Style = Style { ... }
let big: Style = Style { ... }
let attributed: AttributedString = "hello ".set(style: body)
// the following code produce an attributed string by
// concatenating an attributed string and two plain string
// (one styled and another plain).
let attStr = attributed + "\(username)!".set(style:big) + ". You are welcome!"
你还可以使用 +
运算符向纯文本或富文本字符串添加样式
// This produce an attributed string concatenating a plain
// string with an attributed string created via + operator
// between a plain string and a style
let attStr = "Hello" + ("\(username)" + big)
最后,你可以使用函数构建器连接字符串
let bold = Style { ... }
let italic = Style { ... }
let attributedString = AttributedString.composing {
"hello".set(style: bold)
"world".set(style: italic)
}
将样式应用于 String
和 Attributed String
两者 String
和 Attributed String
(即 NSMutableAttributedString
)都有一些方便的方法,允许您通过代码轻松创建和管理属性文本。
字符串实例方法
set(style: String, range: NSRange? = nil)
:通过生成属性字符串,将全局注册的样式应用于字符串(或子字符串)。set(styles: [String], range: NSRange? = nil)
:通过生成属性字符串,将全局注册的样式顺序序列应用于字符串(或子字符串)。set(style: StyleProtocol, range: NSRange? = nil)
:通过生成属性字符串,将Style
或StyleXML
实例(用于渲染基于标签的文本)应用于字符串(或子字符串)。set(styles: [StyleProtocol], range: NSRange? = nil)
:按照顺序将Style
/StyleXML
实例序列应用于字符串(或子字符串),以生成单个属性集合,将应用于字符串(或子字符串)以生成属性字符串。
一些示例
// apply a globally registered style named MyStyle to the entire string
let a1: AttributedString = "Hello world".set(style: "MyStyle")
// apply a style group to the entire string
// commonStyle will be applied to the entire string as base style
// styleH1 and styleH2 will be applied only for text inside that tags.
let styleH1: Style = ...
let styleH2: Style = ...
let StyleXML = StyleXML(base: commonStyle, ["h1" : styleH1, "h2" : styleH2])
let a2: AttributedString = "Hello <h1>world</h1>, <h2>welcome here</h2>".set(style: StyleXML)
// Apply a style defined via closure to a portion of the string
let a3 = "Hello Guys!".set(Style({ $0.font = SystemFonts.Helvetica_Bold.font(size: 20) }), range: NSMakeRange(0,4))
AttributedString 实例方法
类似的方法也适用于属性字符串。
存在三个类别的的方法
set
方法替换目标上已设置的任何现有属性。add
向由样式/样式列表定义的目标添加属性。remove
从接收器字符串中删除定义的属性。
这些方法的每一个都改变属性字符串的接收器实例,并在输出中也返回相同的实例(因此允许链式调用)。
添加
add(style: String, range: NSRange? = nil)
:向字符串/子字符串的现有样式添加全局注册的具有给定名称的样式。add(styles: [String], range: NSRange? = nil)
:向字符串/子字符串的现有样式添加一个样式,该样式是具有给定名称的全局注册样式的顺序序列的总和。add(style: StyleProtocol, range: NSRange? = nil)
:通过更改接收器属性字符串,将传递的样式实例追加到字符串/子字符串中。add(styles: [StyleProtocol], range: NSRange? = nil)
:通过更改接收器属性字符串,将传递的样式的顺序序列追加到字符串/子字符串中。
设置
set(style: String, range: NSRange? = nil)
:用具有给定名称的全局注册样式中的属性替换字符串/子字符串中的任何现有样式。set(styles: [String], range: NSRange? = nil)
:用具有给定名称的全局注册样式的属性合并来替换字符串/子字符串中的任何现有样式。set(style: StyleProtocol, range: NSRange? = nil)
:使用传入的样式实例的属性替换字符串/子字符串中现有的任意样式。set(styles: [StyleProtocol], range: NSRange? = nil)
:使用传入的有序样式序列的属性替换字符串/子字符串中现有的任意样式。
删除
removeAttributes(_ keys: [NSAttributedStringKey], range: NSRange)
:从字符串/子字符串中删除由传入的键指定的属性。remove(_ style: StyleProtocol)
:从字符串/子字符串中删除由样式指定的属性。
示例
let a = "hello".set(style: styleA)
let b = "world!".set(style: styleB)
let ab = (a + b).add(styles: [coupondStyleA,coupondStyleB]).remove([.foregroundColor,.font])
样式中的字体与颜色
你可以为 Style
设置的所有颜色和字体都被 FontConvertible
和 ColorConvertible
协议所封装。
SwiftRichString 显然为 UIColor
/ NSColor
,UIFont
/ NSFont
实现了这些协议,同时为 String
也实现了。对于字体来说,这意味着你可以通过直接提供其 PostScript 名称来分配字体,它将被自动转换为有效的实例。
let firaLight: UIFont = "FiraCode-Light".font(ofSize: 14)
...
...
let style = Style {
$0.font = "Jura-Bold"
$0.size = 24
...
}
在 UIKit 中,你还可以使用 SystemFonts
枚举来从所有可用的 iOS 字体的类型安全自动完成列表中选择。
let font1 = SystemFonts.Helvetica_Light.font(size: 15)
let font2 = SystemFonts.Avenir_Black.font(size: 24)
对于颜色来说,这意味着你可以从十六进制字符串创建有效的颜色实例。
let color: UIColor = "#0433FF".color
...
...
let style = Style {
$0.color = "#0433FF"
...
}
显然,你仍然可以传递颜色/字体的实例。
衍生一个 Style
有时你可能需要从现有的样式推断新样式的属性。在这种情况下,你可以使用 Style
的 byAdding()
函数来生成一个新样式,这个新样式包含接收者的所有属性,并且有机会配置额外的/替换的属性。
let initialStyle = Style {
$0.font = SystemFonts.Helvetica_Light.font(size: 15)
$0.alignment = right
}
// The following style contains all the attributes of initialStyle
// but also override the alignment and add a different foreground color.
let subStyle = bold.byAdding {
$0.alignment = center
$0.color = UIColor.red
}
遵守 Dynamic Type
为了支持你的字体/文本根据用户的偏好内容大小动态缩放,你可以实现样式的 dynamicText
属性。UIFontMetrics
的属性被封装在 DynamicText
类中。
let style = Style {
$0.font = UIFont.boldSystemFont(ofSize: 16.0)
$0.dynamicText = DynamicText {
$0.style = .body
$0.maximumSize = 35.0
$0.traitCollection = UITraitCollection(userInterfaceIdiom: .phone)
}
}
渲染XML/HTML标记字符串
SwiftRichString还支持解析和渲染xml标记字符串以生成有效的NSAttributedString
实例。这对于从远程服务接收动态字符串时需要轻松生成渲染字符串尤其有用。
为了渲染一个XML字符串,您需要创建一个包含您计划在单个StyleXML
实例中渲染的所有样式的组合,并将其应用到您的源字符串,就像您为单个Style
所做的那样。
例如
// The base style is applied to the entire string
let baseStyle = Style {
$0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize * 1.15)
$0.lineSpacing = 1
$0.kerning = Kerning.adobe(-20)
}
let boldStyle = Style {
$0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize)
$0.dynamicText = DynamicText {
$0.style = .body
$0.maximumSize = 35.0
$0.traitCollection = UITraitCollection(userInterfaceIdiom: .phone)
}
}
let italicStyle = Style {
$0.font = UIFont.italicSystemFont(ofSize: self.baseFontSize)
}
// A group container includes all the style defined.
let groupStyle = StyleXML.init(base: baseStyle, ["b" : boldStyle, "i": italicStyle])
// We can render our string
let bodyHTML = "Hello <b>world!</b>, my name is <i>Daniele</i>"
self.textView?.attributedText = bodyHTML.set(style: group)
自定义XML渲染:对标签属性和未知标签做出反应
您还可以向标签添加自定义属性,并以您喜欢的格式渲染它:您需要提供一个XMLDynamicAttributesResolver
协议的具体实现,并将其分配给StyleXML
的.xmlAttributesResolver
属性。
协议会收到两种类型的事件
applyDynamicAttributes(to attributedString: inout AttributedString, xmlStyle: XMLDynamicStyle)
在解析器遇到具有自定义属性的存在样式时收到。将应用样式并调用事件,这样您可以进行进一步的定制。func styleForUnknownXMLTag(_ tag: String, to attributedString: inout AttributedString, attributes: [String: String]?)
在接收到未知标签(未在StyleXML
的样式中定义)时收到。您可以决定忽略或进行定制。
以下示例用于在所有已知的标签中重写文本颜色
// First define our own resolver for attributes
open class MyXMLDynamicAttributesResolver: XMLDynamicAttributesResolver {
public func applyDynamicAttributes(to attributedString: inout AttributedString, xmlStyle: XMLDynamicStyle) {
let finalStyleToApply = Style()
xmlStyle.enumerateAttributes { key, value in
switch key {
case "color": // color support
finalStyleToApply.color = Color(hexString: value)
default:
break
}
}
attributedString.add(style: finalStyleToApply)
}
}
// Then set it to our's StyleXML instance before rendering text.
let groupStyle = StyleXML.init(base: baseStyle, ["b" : boldStyle, "i": italicStyle])
groupStyle.xmlAttributesResolver = MyXMLDynamicAttributesResolver()
以下示例定义了非已知标签rainbow
的行为。
具体来说,它通过为源字符串的每个字母设置自定义颜色来修改字符串。
open class MyXMLDynamicAttributesResolver: XMLDynamicAttributesResolver {
public override func styleForUnknownXMLTag(_ tag: String, to attributedString: inout AttributedString, attributes: [String : String]?) {
super.styleForUnknownXMLTag(tag, to: &attributedString, attributes: attributes)
if tag == "rainbow" {
let colors = UIColor.randomColors(attributedString.length)
for i in 0..<attributedString.length {
attributedString.add(style: Style({
$0.color = colors[i]
}), range: NSMakeRange(i, 1))
}
}
}
}
}
您将在attributes
参数中收到所有阅读标签属性。
您可以在attributedString
属性的inout
参数中更改属性或整个接收到的字符串。
库还提供了一个默认解析器StandardXMLAttributesResolver
,并将其用作默认项。它将支持标签中的color
属性和带有URL链接的<a href>
标签。
let sourceHTML = "My <b color=\"#db13f2\">webpage</b> is really <b>cool</b>. Take a look <a href=\"http://danielemargutti.com\">here</a>"
let styleBase = Style({
$0.font = UIFont.boldSystemFont(ofSize: 15)
})
let styleBold = Style({
$0.font = UIFont.boldSystemFont(ofSize: 20)
$0.color = UIColor.blue
})
let groupStyle = StyleXML.init(base: styleBase, ["b" : styleBold])
self.textView?.attributedText = sourceHTML.set(style: groupStyle)
结果是这个

其中,b
标签的蓝色已被颜色标签属性覆盖,而“here”中的链接是可点击的。
自定义文本转换
有时您可能想在字符串上应用自定义文本转换;例如,您可能想将一些文本以特定样式转换为大写。
为了在 Style
实例中提供自定义文本转换,只需将一个或多个 TextTransform
设置到 Style
的 .textTransforms
属性即可
let allRedAndUppercaseStyle = Style({
$0.font = UIFont.boldSystemFont(ofSize: 16.0)
$0.color = UIColor.red
$0.textTransforms = [
.uppercaseWithLocale(Locale.current)
]
})
let text = "test".set(style: allRedAndUppercaseStyle) // will become red and uppercased (TEST)
TextTransform
是一个枚举,具有预定义的转换集,您也可以提供自己的函数,该函数有一个 String
作为源并另一个 String
作为目标
let markdownBold = Style({
$0.font = UIFont.boldSystemFont(ofSize: 16.0)
$0.color = UIColor.red
$0.textTransforms = [
.custom({
return "**\($0)**"
})
]
})
所有文本转换按您在 textTransform
属性中设置的顺序应用。
文本中的本地 & 远程图像
SwiftRichString 支持本地和远程附加图像以及带属性的文本。
您可以通过一行代码创建带图像的属性字符串
// You can specify the bounds of the image, both for size and the location respecting the base line of the text.
let localTextAndImage = AttributedString(image: UIImage(named: "rocket")!, bounds: CGRect(x: 0, y: -20, width: 25, height: 25))
// You can also load a remote image. If you not specify bounds size is the original size of the image.
let remoteTextAndImage = AttributedString(imageURL: "http://...")
// You can now compose it with other attributed or simple string
let finalString = "...".set(style: myStyle) + remoteTextAndImage + " some other text"
还可以通过渲染包含 img
标签(使用 named
标签表示本地资源,url
表示远程 URL)的 XML 字符串来加载图像。
rect
参数是可选的,允许您指定资源的调整大小和重新定位。
let taggedText = """
Some text and this image:
<img named="rocket" rect="0,-50,30,30"/>
This other is loaded from remote URL:
<img url="https://www.macitynet.it/wp-content/uploads/2018/05/video_ApplePark_magg18.jpg"/>
"""
self.textView?.attributedText = taggedText.set(style: ...)
这就是结果

有时您可能希望延迟提供这些图像。为了做到这一点,只需在 StyleXML
实例中提供对 imageProvider
回调的自定义实现即可
let xmlText = "- <img named=\"check\" background=\"#0000\"/> has done!"
let xmlStyle = StyleXML(base: {
/// some attributes for base style
})
// This method is called when a new `img` tag is found. It's your chance to
// return a custom image. If you return `nil` (or you don't implement this method)
// image is searched inside any bundled `xcasset` file.
xmlStyle.imageProvider = { (imageName, attributes) in
switch imageName {
case "check":
// create & return your own image
default:
// ...
}
}
self.textView?.attributedText = xmlText.set(style: x)
样式管理器
注册全局可用样式
样式可以根据您需要创建或注册为全局样式,以便在需要时使用。第二种方法强烈建议这样做,因为它允许您根据需要为主题应用程序,并避免代码重复。
要全局注册 Style
或 StyleXML
,您需要为其分配一个唯一标识符,并通过 Styles
快捷方式(等同于调用 StylesManager.shared
)调用 register()
函数。
为了使您的代码类型更安全,您可以使用不可实例化的结构来保持样式的名称,然后使用它来注册样式
// Define a struct with your styles names
public struct StyleNames {
public static let body: String = "body"
public static let h1: String = "h1"
public static let h2: String = "h2"
private init { }
}
然后您可以使用
let bodyStyle: Style = ...
Styles.register(StyleNames.body, bodyStyle)
现在您可以在应用程序的任何地方使用它;您可以通过其名称将样式应用于文本
let text = "hello world".set(StyleNames.body)
或者您可以通过 Interface Builder 设计属性将 body
字符串分配给 styledText
。
按需延迟样式创建
有时您可能需要返回仅在您的应用程序的小部分中使用的特定样式;虽然您仍可以直接设置它,您也可以在 StylesManager
中延迟其创建。
通过实现 onDeferStyle()
回调,您可以选择在需要时创建一个新的样式:您将收到该样式的标识符。
Styles.onDeferStyle = { name in
if name == "MyStyle" {
let normal = Style {
$0.font = SystemFonts.Helvetica_Light.font(size: 15)
}
let bold = Style {
$0.font = SystemFonts.Helvetica_Bold.font(size: 20)
$0.color = UIColor.red
$0.backColor = UIColor.yellow
}
let italic = normal.byAdding {
$0.traitVariants = .italic
}
return (StyleXML(base: normal, ["bold": bold, "italic": italic]), true)
}
return (nil,false)
}
以下代码为 myStyle
标识符返回一个有效的样式并对其进行缓存;如果您不想进行缓存,则返回 false
与样式实例一起。
现在您可以使用您的样式来渲染,例如,将基于标签的文本渲染到 UILabel
中:只需设置要使用的样式名称。

使用 Interface Builder 分配样式
SwiftRichString 也可以通过 Interface Builder 使用。
UILabel
UITextView
UITextField
有三个额外的属性
styleName: String
(通过 IB 可用):您可以将它设置为在父视图加载之前已注册的全局样式渲染已设置的文本。style: StyleProtocol
:您可以将它设置为使用样式实例渲染控件中的文本。styledText: String
:使用此属性,而不是attributedText
,设置控件的新文本并使用已设置的样式进行渲染。您还可以继续使用attributedText
并使用String
/AttributedString
的.set()
函数设置值。
指定的样式可以是 Style
、StyleXML
或 StyleRegEx
。
- 如果样式是
Style
,控件的所有文本都将用样式定义的属性设置。 - 如果样式是
StyleXML
,则设置基本属性(如果base
有效),并且其他属性一旦找到每个标记就会应用。 - 如果样式是
StyleRegEx
,则设置基本属性(如果base
有效)并且仅对指定模式的匹配应用属性。
通常,您将通过 IB 中的 Style Name
(styleName
)属性设置标签的样式,并通过设置 styledText
来更新控件的内容。
// use `styleName` set value to update a text with the style
self.label?.styledText = "Another text to render" // text is rendered using specified `styleName` value.
否则,您可以手动设置值。
// manually set the an attributed string
self.label?.attributedText = (self.label?.text ?? "").set(myStyle)
// manually set the style via instance
self.label?.style = myStyle
self.label?.styledText = "Updated text"
通过 Style
类可用的属性
以下属性可用:
属性 | 类型 | 描述 |
---|---|---|
size | CGFloat |
字体大小,以点为单位 |
font | FontConvertible |
用于文本的字体 |
color | ColorConvertible |
文本前景色 |
backColor | ColorConvertible |
文本背景色 |
shadow | NSShadow |
文本的阴影效果 |
underline | (NSUnderlineStyle?,ColorConvertible?) |
下划线样式和颜色(如果颜色为nil,则使用前景色) |
strikethrough | (NSUnderlineStyle?,ColorConvertible?) |
strikethrough样式和颜色(如果颜色为nil,则使用前景色) |
baselineOffset | Float |
字符从底线起偏移量,以点为单位 |
paragraph | NSMutableParagraphStyle |
段落属性 |
lineSpacing | CGFloat |
一行底部和下一行顶部之间的距离,以点为单位 |
paragraphSpacingBefore | CGFloat |
段落顶部和其文本内容开始之间的距离 |
paragraphSpacingAfter | CGFloat |
段落末尾添加的空间(以点为单位) |
alignment | NSTextAlignment |
接收器的文本对齐方式 |
firstLineHeadIndent | CGFloat |
文本容器左边缘与段落第一行开始的距离(以点为单位) |
headIndent | CGFloat |
文本容器左边缘与除第一行之外的行的开始的距离(以点为单位) |
tailIndent | CGFloat |
此值是左边缘的距离,如果为0或负数,则是从右边缘的距离。 |
lineBreakMode | LineBreak |
用于断行的模式 |
minimumLineHeight | CGFloat |
接收器中的任何一行占用的最小高度,以点为单位,无论字体大小或附加图形的大小如何 |
maximumLineHeight | CGFloat |
接收器中的任何一行占用的最大高度,以点为单位,无论字体大小或附加图形的大小如何 |
baseWritingDirection | NSWritingDirection |
用于确定文本实际书写方向的初始书写方向 |
lineHeightMultiple | CGFloat |
接收器的自然行高乘以这个因子(如果为正数)之后,被最小和最大行高所限制 |
hyphenationFactor | Float |
控制何时尝试连字符拆分的阈值 |
ligatures | Ligatures |
连字符将特定字符组合渲染为与这些字符对应的单个自定义符号 |
speaksPunctuation | Bool |
启用文本的语音提示 |
speakingLanguage | String |
说话时使用的语言(值是BCP 47语言代码字符串) |
speakingPitch | Double |
应用于语音内容的高低音 |
speakingPronunciation | String |
|
shouldQueueSpeechAnnouncement | Bool |
语音文本将排队在现有语音内容之后,或中断现有语音内容 |
headingLevel | HeadingLevel |
指定文本的标题级别 |
numberCase | NumberCase |
数字格式的配置,也称为“数字样式” |
numberSpacing | NumberSpacing |
数字间距的配置,也称为“数字间距” |
fractions | Fractions |
显示分数的配置 |
superscript | Bool |
上标(上标)符号变体用于,如在脚注_中。 |
subscript |
Bool |
下标(下标)符号变体用于:v_。 |
ordinals | Bool |
序数符号变体用于,如在常见的排印中4th。 |
scientificInferiors | Bool |
Scientific inferior符号变体用于:H_O |
smallCaps | Set<SmallCaps> |
配置小写样式行为。 |
stylisticAlternates | StylisticAlternates |
用于自定义字体的不同风格替代选项。 |
contextualAlternates | ContextualAlternates |
用于自定义字体的不同上下文替代选项。 |
kerning | Kerning |
要应用的字距微调。 |
traitVariants | TraitVariant |
描述要应用于字体的特征变体 |
要求
- Swift 5.1+
- iOS 8.0+
- macOS 11.0+
- watchOS 2.0+
- tvOS 11.0+
安装
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。有关使用和安装说明,请访问他们的网站。要使用 CocoaPods 将 Alamofire 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它。
pod 'SwiftRichString'
Swift 包管理器
Swift 包管理器 是一个用于自动化 Swift 代码发布的工具,并集成到 Swift 编译器中。它处于早期开发阶段,但 Alamofire 支持在支持的平台上使用它。
设置完您的 Swift 包后,将 Alamofire 添加为依赖项就像将其添加到您的 Package.swift 的 dependencies 值中一样简单。
dependencies: [
.package(url: "https://github.com/malcommac/SwiftRichString.git", from: "3.5.0")
]
Carthage
Carthage 是一个去中心化的依赖管理器,它构建您的依赖并提供二进制框架。
要使用 Carthage 将 SwiftRichString 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定它。
github "malcommac/SwiftRichString"
运行 carthage
构建 framework,并将构建的 SwiftRichString.framework
拖放到您的 Xcode 项目中。
贡献
版权
SwiftRichString受MIT许可证的许可。有关更多信息,请参阅LICENSE文件。
Daniele Margutti: [email protected], @danielemargutti