【发布时间】:2020-02-22 12:29:18
【问题描述】:
我是 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()
}
}
}
基于布尔决策将选择器的样式从 DefaultPickerStyle 更改为 SegmentedPickerStyle 时,我遇到了同样的问题。我需要这个来让用户界面更加用户友好。
任何想法如何实现这一点?
【问题讨论】:
标签: swift properties modifier