字符串
SwiftUI 中涉及到字符串的地方,基本都支持普通的字符串和本地化字符串。
比如 Text 包括如下初始化方法:
1
2
3
4
5
6
7
8
9
10
/// 普通字符串
init < S >( _ content : S ) where S : StringProtocol
/// 本地化字符串
init (
_ key : LocalizedStringKey ,
tableName : String ? = nil ,
bundle : Bundle ? = nil ,
comment : StaticString ? = nil
)
比如 Label 包括如下初始化方法:
1
2
3
4
5
6
7
8
9
10
11
/// 普通字符串
init < S >(
_ title : S ,
image name : String
) where S : StringProtocol
/// 本地化字符串
init (
_ titleKey : LocalizedStringKey ,
image name : String
)
如下是一个简单的 Text 示例代码,效果查看示例。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private let s1 = "Stay Hungry, Stay Foolish!"
@ State private var lineLimit = 1
@ State private var truncationMode : Text . TruncationMode = . tail
@ State private var multilineTextAlignment : TextAlignment = . leading
@ State private var reservesSpace = true
Text ( Array ( repeating : s1 , count : 5 ). joined ())
// .lineLimit(3)
. lineLimit ( lineLimit , reservesSpace : reservesSpace ) // 显示空行占位空间
. multilineTextAlignment ( multilineTextAlignment ) // 多行文字对齐方式
. truncationMode ( truncationMode ) // 文字截断魔术
. textSelection (. enabled ) // iOS 15 新增,长按可复制
Toggle ( "reservesSpace" , isOn : $ reservesSpace )
Stepper ( "lineLimit: \( lineLimit ) " , value : $ lineLimit , in : 1. .. 6 )
Picker ( "truncationMode" , selection : $ truncationMode ) {
ForEach ( Text . TruncationMode . allCases , id : \ . self ) { v in
Text ( v . text )
}
}
Picker ( "multilineTextAlignment" , selection : $ multilineTextAlignment ) {
ForEach ( TextAlignment . allCases , id : \ . self ) { v in
Text ( v . text )
}
}
> 查看示例
格式化
初始化方法 init(_:format:) 可以传入 FormatStyle 类型的 format 参数对数据进行格式化。
比如要将一个数字转换成百分比:
1
2
3
Text ( 0.1230 , format : . percent )
// 等同于
// Text(0.1230.formatted(.percent))
或是展示当前日期和时间:
1
2
3
Text ( Date . now , format : . dateTime )
// 等同于
// Text(Date.now.formatted())
甚至是展示今天是今年的第几天或第几周:
1
2
3
Text ( Date . now , format : Date . FormatStyle (). dayOfYear ())
Text ( Date . now , format : Date . FormatStyle (). week ())
乃至将数组直接以 Text 形式展示:
1
2
3
Text ([ "A" , "B" , "C" ], format : . list ( type : . and , width : . narrow ))
Text ([ 1 , 2 , 3 ], format : . list ( memberStyle : . number , type : . or ))
如此种种,无法一一例举,请读者自行探究。
富文本
Text 支持通过 Image 构建,它还实现了操作符重载,这意味着我们可以通过字符串插值和运算符 + 实现简单的图文混排:
1
2
3
4
Text ( " \( Image ( systemName : "applelogo" )) Apple " )
. foregroundColor (. blue ). bold () +
Text ( " \( Image ( systemName : "swift" )) Swift" )
. foregroundColor (. orange ). italic ()
Text 也支持通过 AttributedString 初始化 来实现富文本。
Markdown
iOS 15.0 新增了对 Markdown 文本的的支持:
1
Text ( "**This** ~~is~~ an *example* of ***Markdown*** `Text`, [See More](https://developer.apple.com/documentation/swiftui/text)" )
目前 Text 对 Markdown 的支持比较有限,仅能渲染一些简单的样式。
渐变文字
.foregroundStyle 可以改变文字、图片等的样式,比如要给文字添加渐变色:
1
2
3
4
5
6
Text ( s1 )
. font (. title3 ). bold ()
. foregroundStyle (. linearGradient (
colors : [. orange , . yellow , . blue , . purple ],
startPoint : . topLeading , endPoint : . bottomTrailing )
)
foregroundStyle 有如下三个方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 1
func foregroundStyle < S >( _ style : S ) -> some View where S : ShapeStyle
// 2
func foregroundStyle < S1 , S2 >(
_ primary : S1 ,
_ secondary : S2
) -> some View where S1 : ShapeStyle , S2 : ShapeStyle
// 3
func foregroundStyle < S1 , S2 , S3 >(
_ primary : S1 ,
_ secondary : S2 ,
_ tertiary : S3
) -> some View where S1 : ShapeStyle , S2 : ShapeStyle , S3 : ShapeStyle
这里的泛型均遵循 ShapeStyle 协议,而 ShapStyle 遵循 HierarchicalShapeStyle 协议,它有四个样式依次减弱的等级供选择,就是如下代码中的 primary(默认),secondary,tertiary 和 quaternary,我们可以使用这个等级自动渲染不同的样式。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
HStack {
// 只设置一种 style,会自动依次减弱
foregroundStyleView
// .foregroundColor(.blue) // 效果等同于下面代码
. foregroundStyle (. blue )
// 设置了多种 style,会根据顺序渲染
// 如果超出设置的 style 数量(最多三种)
// 会使用最后一种 style
foregroundStyleView
. foregroundStyle (. blue , . red , . purple )
foregroundStyleView
. foregroundStyle (. blue , . red )
}
var foregroundStyleView : some View {
VStack {
Label ( "Apple" , image : "applelogo" )
// .foregroundStyle(.primary) // 默认
Label ( "Apple" , image : "applelogo" )
. foregroundStyle (. secondary )
Label ( "Apple" , image : "applelogo" )
. foregroundStyle (. tertiary )
Label ( "Apple" , image : "applelogo" )
. foregroundStyle (. quaternary )
}
}
foregroundStyle 用于改变视图的前景样式,与之相对的是 backgroundStyle(_:) ,它可以为视图添加背景样式,二者的使用方法是类似的。
日期
Text 可以直接展示日期,创建一个计时器只需要一行代码就可以实现:
1
Text (. now , style : . timer )
style 支持多种样式,效果见示例。其中 .offset、.relative 和 .timer 展示的时间都是根据秒数动态变化的,其它样式的日期则是静态的。