小编Enr*_*ico的帖子

Swift:text() .font 的条件修饰符

我是 Swift 的新手,仍在学习中。我尝试根据布尔变量格式化文本。它与字体大小和样式配合得很好。但它不适用于 .bold() 或 .italic 等样式。知道如何做到这一点吗?我也尝试了ViewModifier,但存在同样的问题。

struct ContentView: View {
    @State private var txtFont = false

    var body: some View {
        VStack{
            Spacer()
            Button("Toggle the Textproperty") {
                self.txtFont.toggle()
            }
            Spacer()
            Text("Hello, World!")
                .font(txtFont ? .largeTitle : .none)
                .bold()
                .italic()
//                txtFont ? .bold() : .none <= this line won't work
            Spacer()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

DefaultPickerStyle我在将选择器的样式从基于布尔决策更改SegmentedPickerStyle为基于布尔决策时遇到同样的问题。我需要这个来让用户界面更加用户友好。

有什么想法如何实现这一点?

properties modifier swift

2
推荐指数
1
解决办法
1206
查看次数

标签 统计

modifier ×1

properties ×1

swift ×1