SwiftUI 视图使用 cornerRadius() 修饰符将其角圆化的代码演示

11 min read

以下代码演示了如何使用 cornerRadius() 修饰符将 SwiftUI 视图的角进行圆化。

import SwiftUI

struct RoundedView: View {
    var body: some View {
        Text("Hello, SwiftUI!")
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(10)
    }
}

struct RoundedView_Previews: PreviewProvider {
    static var previews: some View {
        RoundedView()
    }
}

在上面的代码中,我们使用 Text 视图创建文本,在 padding() 修饰符中添加填充,然后使用 background()foregroundColor() 修饰符设置背景色和前景色。最后,我们使用 cornerRadius() 修饰符将其角圆化为半径为 10 的圆角。

您可以将此代码添加到 Xcode 中并查看效果。我们建议您在 RoundedView_Previews 中使用预览,因为它提供了实时预览。