展示文本的控件,类似UIKit中的UILabel,常用Api如下:

    1. @frozen public struct Text : Equatable {
    2. /// 带一串字符的初始化
    3. public init<S>(_ content: S) where S : StringProtocol
    4. }
    5. extension Text {
    6. /// 文字颜色
    7. public func foregroundColor(_ color: Color?) -> Text
    8. /// 字体
    9. public func font(_ font: Font?) -> Text
    10. /// 字重
    11. public func fontWeight(_ weight: Font.Weight?) -> Text
    12. /// 加粗
    13. public func bold() -> Text
    14. /// 斜体
    15. public func italic() -> Text
    16. /// 数字占位同等,也就是1和9所占宽度相等
    17. @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
    18. public func monospacedDigit() -> Text
    19. /// 删除线
    20. public func strikethrough(_ active: Bool = true, color: Color? = nil) -> Text
    21. /// 下划线
    22. public func underline(_ active: Bool = true, color: Color? = nil) -> Text
    23. /// 字间距
    24. public func kerning(_ kerning: CGFloat) -> Text
    25. }

    示例如下:
    image.png

    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()
        }
    }