What's new in Foundation
1、Apple 专为 Swift 打造了一个新的字符串类型 AttributedString
2、日期和数字格式化的改进
一、属性字符串 - AttributedString
在 Foundation 框架面世之时,NSAttributedString 类型便随之一起推出。而今年,Apple 推出了一个基于现代化的 Swift 语言特性的全新的结构体 - AttributedString。
- 1.1 AttributedString基础
AttributedString是由字符(Characters)、区间(Ranges)、字典(Dictionary)3部分组成。
![](https://img.haomeiwen.com/i13603655/2bb15b2083cd5427.png)
通过简单初始化得到AttributedString结构体,设置font,foregroundColor,link等属性。
AttributeContainer可以根据不同的AttributedString设置不同的属性。
通过调用 mergeAttributes 方法,将携带的属性应用在不同的属性字符串上。
- 1.2 AttributedString View
访问AttributedString的组成的三部分,并不是通过访问AttributedString自身,而是通过View。View由两部分组成:Characters(字符,用来访问字符串)、Runs(用来访问属性)。
Characters View
![](https://img.haomeiwen.com/i13603655/e00e942c17083da2.png)
首先通过 characters 取出字符 View
遍历字符 View,并通过 isPunctuation 筛选出字符 View 集合中为标点符号的字符,然后设置其属性
Runs View
![](https://img.haomeiwen.com/i13603655/5e4b2f85f4e07993.png)
一个 Run View 包含了一个特定的属性的起始位置、长度、以及具体的属性值。
由图中可以看出 Thank you! Please visit our website. 分别是由"Thank you"、"!"、"Please visit our website"、"." 4部分组成。
-
1.3 AttributedString & Markdown
Markdown.png
由上图可以看到,AttributedString 支持 Markdown 语法
- 1.4 AttributedString 转换操作
从结构体类型转换为类类型
直接将AttribuedString 传入 NSAttributedString 的构造方法中
var attributedString111 = AttributedString("This is a basic string.")
attributedString111.inlinePresentationIntent = .stronglyEmphasized
label.attributedText = NSAttributedString(attributedString111)
序列化和反序列化
![](https://img.haomeiwen.com/i13603655/5537ba89e09f1c98.png)
因为 AttribuedString 有默认的 Codable 实现,这里的 TestAttr 结构体只需要遵循 Codable 协议即可
二、格式化器 - Formatters
![](https://img.haomeiwen.com/i13603655/5922f43f33d256a1.png)
通过上面的示例代码,我们可以看到,原来的我们使用日期格式化器的时候,需要创建一个Formatter,然后传给Formatter一个日期,返回一个格式化字符串。iOS15之后,有了新的Formatters 语法
2.1 新的 Formatters 语法
![](https://img.haomeiwen.com/i13603655/ce03ebebd910f09d.png)
在最新的 Formatters API 发布后,我们无需再手动创建并配置 Formatter,同时,我们也不需要传给 Formatter 一个日期对象了,我么只需要在日期对象身上调用 formatted 方法,并指定格式化标准是什么。
上面的新Formatters API 更容易理解,并且可读性,可维护性也更高。
2.2 日期格式化
let date = Date.now
var formatted = date.formatted()
// 9/30/2021, 3:31 PM
let standardDate = date.formatted(date: .abbreviated, time: .standard)
// Sep 30, 2021, 3:31:07 PM
let onlyDate = date.formatted(date: .numeric, time: .omitted)
// 9/30/2021
let onlyTime = date.formatted(date: .omitted, time: .shortened)
// 3:31 PM
let formatter1 = date.formatted(.dateTime.year().day().month())
// Sep 30, 2021
let formattedWide = date.formatted(.dateTime.year().day().month(.wide))
// September 30, 2021
let formattedWeekday = date.formatted(.dateTime.weekday(.wide))
// Thursday
let logFormat = date.formatted(.iso8601)
// 2021-09-30T07:31:07Z
let fileNameFormat = date.formatted(.iso8601.year().month().day().dateSeparator(.dash))
// 2021-09-30
let later = date + TimeInterval(6000)
let range = (date..<later).formatted()
// 9/30/21, 3:50 – 5:14 PM
let noDate = (date..<later).formatted(date: .omitted, time: .complete)
// 3:50:43 PM GMT+8 – 5:14:03 PM GMT+8
let timeDuration = (date..<later).formatted(.timeDuration)
// 1:23:20
let components = (date..<later).formatted(.components(style: .wide))
// 1 hour, 23 minutes, 20 seconds
let relative = later.formatted(.relative(presentation: .named, unitsStyle: .wide))
// in 1 hour
let array: [String] = [formatted, standardDate, onlyDate, onlyTime, formatter1, formattedWide, formattedWeekday, logFormat, fileNameFormat, range, noDate, timeDuration, components, relative]
var temp = ""
for str in array {
temp += str + "\n"
}
print("temp = \(temp)")
formatLabel.text = temp
![](https://img.haomeiwen.com/i13603655/b1cf2d5dad8a0453.png)
新的 Formatters API 的一个重要的目标是在于创建正确的格式化时提供尽可能多的编译时支持。通过指定 formatted 函数的参数,可以获得不同格式的时间表示方式。代码通过链式调用的方式指明需要展示年月日的信息,最终结果将会根据用户的语言环境 locale 输出对应的内容。字段的顺序并不影响,每个字段都只是告诉格式化器在最终的输出结果中应该包含什么样的值。
2.3 数字格式化
let value = 12345
print("value = \(value.formatted())") // 12,345
let percentValue = 30
print("percentValue = \(percentValue.formatted(.percent))") // 30%
let price = 19
print("price = \(price.formatted(.currency(code: "usd")))") // $19.00
let scientific = 42e9
print("scientific = \(scientific.formatted(.number.notation(.scientific)))") // 4.2E10
let list = [25, 50, 75].formatted(.list(memberStyle: .percent, type: .and))
print("list = \(list)") // 25% 50% 75%
数字格式化支持各种配置,我们可以配置输出百分比的字符串,或者是科学记数法格式的字符串,亦或者是输出货比格式的内容。
总结
- AttributedString 提供了一个快速的,易用的并且 Swift 优先的接口,进而实现在一个字符串的范围中添加键值对以达到富文本的效果。你可以在 SwiftUI 中使用 Text 组件,并在本地化字符串中使用 Markdown 语法。
- 新的格式器 API 将重点放在格式上,简化了代码并提高了性能。
What's new in UIKit
- UIButtonConfiguration
我们也可以单独定义 UIButtonConfiguration 实例,并设置一些诸如文本、图片等属性值,再使用这个实例来创建按钮
![](https://img.haomeiwen.com/i13603655/fdbe9c6761ca30f5.png)
var config = UIButton.Configuration.tinted()
config.title = "测试按钮"
config.image = UIImage(named: "close")
config.imagePlacement = .trailing
// config.showsActivityIndicator = true
let configBtn = UIButton(configuration: config, primaryAction: nil)
![](https://img.haomeiwen.com/i13603655/4145c7315ebdab2f.png)
另外,我们还可以设置按钮的 configurationUpdateHandler,让按钮在状态发生改变时自动去执行一些操作
configBtn.configurationUpdateHandler = { btn in
if var config = btn.configuration {
config.image = btn.isHighlighted ? UIImage(named: "close") : UIImage(named: "back")
btn.configuration = config
}
}
config.showsActivityIndicator = true
![](https://img.haomeiwen.com/i13603655/74c344be0590c1cb.png)
要调整按钮布局也很方便,只需要修改一些属性值即可,像 baseBackgroundColor、baseForegroundColor、cornerStyle 和 buttonSize 等等。
![](https://img.haomeiwen.com/i13603655/5d0fba641c358173.png)
![](https://img.haomeiwen.com/i13603655/d796a59cf9a3fe33.png)
- Toggle 按钮
-
Pop-up 按钮
PopBtn.png
- UIMenu
网友评论