使用SwiftUI给文本设置圆角背景

PHOTO EMBED

Wed Feb 01 2023 05:47:27 GMT+0000 (Coordinated Universal Time)

Saved by @zelda #swift #swiftui

//
//  ButtonText.swift
//  SwiftUIDemo
//
//  Created by shiyanjun on 2023/2/1.
//

import SwiftUI

struct ButtonText: View {
    var body: some View {
        VStack{
            // 给文本设置圆角矩形背景
            Text("Hello!Hello!Hello!")
                .foregroundColor(.primary)
                .padding()
                .background(
                    Rectangle()
                        .foregroundColor(Color.purple)
                        .cornerRadius(10)
                )
            // 给文本设置胶囊背景
            Text("Hello!Hello!Hello!")
                .foregroundColor(.primary)
                .padding()
                .background(
                    Capsule()
                        .foregroundColor(Color.purple)
                        .cornerRadius(10)
                )
            // 给文本设置圆形背景
            Text("Hello!Hello!Hello!Hello!")
                .frame(width: 50, height: 50)
                .foregroundColor(.primary)
                .padding()
                .background(
                    Circle()
                        .foregroundColor(Color.purple)
                        .cornerRadius(10)
                )
        }
    }
}

struct ButtonText_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView {
            ButtonText()
        }
        .environment(\.colorScheme, .light)
    }
}
content_copyCOPY