展示文本的控件,类似UIKit中的UILabel,常用Api如下:
@frozen public struct Text : Equatable {/// 带一串字符的初始化public init<S>(_ content: S) where S : StringProtocol}extension Text {/// 文字颜色public func foregroundColor(_ color: Color?) -> Text/// 字体public func font(_ font: Font?) -> Text/// 字重public func fontWeight(_ weight: Font.Weight?) -> Text/// 加粗public func bold() -> Text/// 斜体public func italic() -> Text/// 数字占位同等,也就是1和9所占宽度相等@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)public func monospacedDigit() -> Text/// 删除线public func strikethrough(_ active: Bool = true, color: Color? = nil) -> Text/// 下划线public func underline(_ active: Bool = true, color: Color? = nil) -> Text/// 字间距public func kerning(_ kerning: CGFloat) -> Text}
示例如下:
import SwiftUI
struct TextDemo: View {
var body: some View {
VStack {
Text("迪迦").foregroundColor(.red)
Text("奈克瑟斯").foregroundColor(.red).font(.system(.title3))
Text("戴拿").foregroundColor(.red).font(.system(.title3)).bold()
Text("盖亚").foregroundColor(.red).font(.system(.title3)).italic()
Text("高斯123456").foregroundColor(.red).font(.system(.title3)).italic().monospacedDigit()
Text("梦比优斯").foregroundColor(.red).font(.system(.title3)).italic().strikethrough()
HStack {
Text("早")
Text("田")
Text("他")
Text("们")
}
HStack {
Text("对奥特一族的新人未来提出了很多建议和忠告!")
.foregroundColor(.blue)
Text("我们奥特曼并不是神灵,既有不能挽救的生命,也有无法到达的所想”。")
.foregroundColor(.green)
Text("团说:“最重要的是不要放弃,变不可能为可能,这就是奥特曼")
.foregroundColor(.blue)
}
}
.navigationTitle(Text("Text示例"))
}
}
struct TextDemo_Previews: PreviewProvider {
static var previews: some View {
TextDemo()
}
}
