WWDC2021 - What's new in Foundation
AttributedString
现在,Swift拥有了自己的富文本,以及新的一套API。
AttributedString的基础组成: characters ranges dictionary。
在AttributedString中,可以直接访问和修改对应属性。

var attributedString = AttributedString("This is a basic string that contains a link.")
let range = attributedString.range(of: "link")!
attributedString[range].link = URL(string: "https://www.example.com")
另一个工具叫做属性容器AttributedContainer,可以提前设置好一组属性,然后通过merge
让它生效

Views
AttributedString由characters ranges dictionary组成,但又不是它们中的一个,为了处理这些属性,它通过两个view来帮助处理。
Characters
Characters负责提供对字符串的处理。
AttributedString可以通过.characters
来获取subString
,进而使用String相关的特性


Runs
Runs负责对字符串属性的处理
The attributed runs of the attributed string, as a view into the underlying string.

Markdown
AttributedString现在支持直接使用markdown语法来书写了
do {
let thankYouString = try AttributedString(
markdown:"**Thank you!** Please visit our [website](https://example.com)")
} catch {
print("Couldn't parse: \(error)")
}

除了常用的语法,还支持自定义

Localization
AttributedString可以像String一样做本地化配置


除了以上特性,AttributedString还可以自定义Attribute Scopes,加入了对JSON5的支持,可以自定义Codable,在model与数据间转换
Formatter
新版本的Formatter直接内置到了不同的数据类型里

Date
现在处理Date格式时,我们会分开两部分来处理

在新的API里,可以直接写到一起,而且更加直观

通过选择不同的Type,可以显示不同的样式


时间间隔

字符串转时间

Number

Grammar agreement
在国际化适配上的新feature
待补充
网友评论