美文网首页
SwiftUI学习-基础组件(Text)

SwiftUI学习-基础组件(Text)

作者: 编程的蚂蚁 | 来源:发表于2023-04-25 09:53 被阅读0次

基础组件

Text 文本

// Text 常用修饰器
struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, world!")
                .font(.largeTitle) // 字体,largeTitle为系统提供的一种大标题字体style
                .font(Font.system(size: 28)) // 字体,设置系统字体28号
            
                .bold() // 加粗
                .italic() // 斜体
                .foregroundColor(.yellow) // 文字颜色
                .fontWeight(.heavy) // 文字粗细
            
                .padding(20) // 内间距, 一个数表示上下左右都是20
                .padding(EdgeInsets.init(top: 20, leading: 20, bottom: 20, trailing: 20))
                .padding(.horizontal, 20)
                .padding(.vertical, 20)
                .padding(.top, 20)
            
                .background(
                    Rectangle().foregroundColor(.pink)
                        .cornerRadius(12)
                ) // 使用background修饰器实现圆角

                .background(Color.pink)
                .cornerRadius(12) // 使用cornerRadius设置Text圆角

                .background(in: Circle()) // Text变成圆形蓝色渐变
                .backgroundStyle(.blue.gradient)
            
                .background(
                    Capsule()
                        .strokeBorder(lineWidth: 1)
                        .foregroundColor(.red)
                ) // 设置一个红色的宽度为1的边框
            
                .background(in: Capsule()) // 左右圆角,胶囊状
            
                .onTapGesture {
                    print("Click The Text")
                } // 添加点击手势
                .shadow(radius: 8) // 设置阴影
                .multilineTextAlignment(.leading) // 文字对齐方式
                .lineLimit(2) // 文字行数
            
                .frame(maxWidth: 180) // Text最大宽度
                .frame(width: 100, height: 100, alignment: .center) // 设置宽高和对齐方式
                .frame(minWidth: 100, idealWidth: 120, maxWidth: 140, minHeight: 100, idealHeight: 120, maxHeight: 140, alignment: .leading) //  设置最小宽(高)度,理想宽(高)度,最大宽(高)度,对齐方式(左对齐)
            
                .layoutPriority(1) // 布局优先级,多行文本被显示省略号的时候,可以设置这个使Text完全显示
            
                .offset(x: 150, y: 40) // 偏移
        }
        .padding()
    }

实际开发遇到的问题:
1.当Text组件显示一个url时候,会默认显示蓝色字体,并且点击能跳转到系统浏览器,如何处理?


问题

解决办法:


处理

相关文章

网友评论

      本文标题:SwiftUI学习-基础组件(Text)

      本文链接:https://www.haomeiwen.com/subject/qhgujdtx.html