Text常用API一览
1年前 • 797次点击 • 来自 移动端
标签: SwiftUI
以下是SwiftUI中Text一些简单API使用Demo:
设置字体
Text("Stay Hungry.Stay Foolish.")
.fontWeight(.bold)
//fontWeight可选 black bold heavy light medium等等
设置字体大小
Text("Stay Hungry.Stay Foolish.")
.fontWeight(.bold)
.font(.title)
//font可选 largeTitle title headline subheadline body callout footnote caption等等
使用.system设置字体
Text("Stay Hungry.Stay Foolish.")
.font(.system(size: 25, design: .rounded))
//design可选 serif rounded monospaced
字体颜色
Text("Stay Hungry.Stay Foolish.")
.foregroundColor(.red)
使用.custom自定义字体
Text("Stay Hungry.Stay Foolish.")
.font(.custom("Helvetica Neue", size: 24))
.foregroundColor(.red)
处理多行
Text("Stay Hungry.Stay Foolish.Stay Hungry.Stay Foolish.Stay Hungry.Stay Foolish.Stay Hungry.Stay Foolish.Stay Hungry.Stay Foolish.Stay Hungry.Stay Foolish.Stay Hungry.Stay Foolish.Stay Hungry.Stay Foolish.Stay Hungry.Stay Foolish.Stay Hungry.Stay Foolish.")
.font(.title)
.multilineTextAlignment(.center)//居中对齐
.lineLimit(5)//限制显示5行,多余字符以...代替
.truncationMode(.head)//多余字符以...代替出现的位置放置在头部而不是尾部
显示所有字符
.lineLimit(nil)
行间距
.lineSpacing(10)
Rotation Effect旋转效果
.rotationEffect(.degrees(20), anchor: UnitPoint(x: 0, y: 0))
Rotation Effect 3D旋转效果
.rotation3DEffect(.degrees(60), axis: (x: 1, y: 0, z: 0))
阴影效果
.shadow(color: .gray, radius: 2, x: 0, y: 10)