SwiftUI:数组内的修饰符
SwiftUI: Modifier inside Array
我目前正在尝试将所有可能的修饰符直接放入数组中,然后通过 forEach 调用它们。它也适用于它们中的大多数,不幸的是框架(高度、宽度、对齐方式)不是我想要的。文本和框架应同时对齐。但无论我如何尝试,它都不起作用。此外,我也未能通过这种方式整合 .bold 和 .italic。
我知道还有其他方法。如果可能的话,我希望能够以这种方式包含所有修饰符。这可能吗?
import SwiftUI
struct Tests: Identifiable {
var id: Int
var text: String
var textcolor: Color
var textfont: Font
var height: CGFloat
var width: CGFloat
var alignment: Alignment
var textbackcolor: Color
var corner: CGFloat
var show: Bool
var user: String
}
struct ContentView: View {
@State private var testvar = [
Tests(id: 000, text: "Hello World 1", textcolor: .red, textfont: .title, height: 100, width: 300, alignment: .trailing, textbackcolor: .blue, corner: 20, show: true, user: "System"),
Tests(id: 001, text: "Hello World 2", textcolor: .green, textfont: .largeTitle, height: 100, width: 300, alignment: .center, textbackcolor: .black, corner: 50, show: true, user: "George"),
]
var body: some View {
NavigationView {
VStack {
ForEach(self.testvar) { txt in if txt.show == true {
Text("\(txt.text)")
.frame(width: txt.width, height: txt.height, alignment: txt.alignment)
.font(txt.textfont)
.foregroundColor(txt.textcolor)
.background(txt.textbackcolor)
.cornerRadius(txt.corner)
//.bold?
//.italic?
} }
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
更改修饰符的顺序(斜体或粗体必须仅应用于文本)
ForEach(self.testvar) { txt in if txt.show == true {
Text("\(txt.text)")
.bold()
.italic()
.frame(width: txt.width, height: txt.height, alignment: txt.alignment)
.font(txt.textfont)
.foregroundColor(txt.textcolor)
.background(txt.textbackcolor)
.cornerRadius(txt.corner)
}
}
关于数据
@State private var testvar = [
Tests(id: 000, text: "Hello World 1", textcolor: .red, textfont: .title, height: 100, width: 300, alignment: .trailing, textbackcolor: Color(.blue).opacity(0.2), corner: 20, show: true, user: "System"),
Tests(id: 001, text: "Hello World 2", textcolor: .green, textfont: .largeTitle, height: 300, width: 300, alignment: .center, textbackcolor: .black, corner: 50, show: true, user: "George"),
]
我有
如果您想知道为什么 .largeTitle 不是斜体,请参阅
How to apply .italic() to .largeTitle Font?
更新以反映讨论
为了能够根据数组中的值修改样式,请使用简单的文本扩展
extension Text {
func style(bold: Bool = false, italic: Bool = false) -> Self {
var s = self
if bold {
s = s.bold()
}
if italic {
s = s.italic()
}
return s
}
}
这是(我希望如此)不言自明
我目前正在尝试将所有可能的修饰符直接放入数组中,然后通过 forEach 调用它们。它也适用于它们中的大多数,不幸的是框架(高度、宽度、对齐方式)不是我想要的。文本和框架应同时对齐。但无论我如何尝试,它都不起作用。此外,我也未能通过这种方式整合 .bold 和 .italic。 我知道还有其他方法。如果可能的话,我希望能够以这种方式包含所有修饰符。这可能吗?
import SwiftUI
struct Tests: Identifiable {
var id: Int
var text: String
var textcolor: Color
var textfont: Font
var height: CGFloat
var width: CGFloat
var alignment: Alignment
var textbackcolor: Color
var corner: CGFloat
var show: Bool
var user: String
}
struct ContentView: View {
@State private var testvar = [
Tests(id: 000, text: "Hello World 1", textcolor: .red, textfont: .title, height: 100, width: 300, alignment: .trailing, textbackcolor: .blue, corner: 20, show: true, user: "System"),
Tests(id: 001, text: "Hello World 2", textcolor: .green, textfont: .largeTitle, height: 100, width: 300, alignment: .center, textbackcolor: .black, corner: 50, show: true, user: "George"),
]
var body: some View {
NavigationView {
VStack {
ForEach(self.testvar) { txt in if txt.show == true {
Text("\(txt.text)")
.frame(width: txt.width, height: txt.height, alignment: txt.alignment)
.font(txt.textfont)
.foregroundColor(txt.textcolor)
.background(txt.textbackcolor)
.cornerRadius(txt.corner)
//.bold?
//.italic?
} }
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
更改修饰符的顺序(斜体或粗体必须仅应用于文本)
ForEach(self.testvar) { txt in if txt.show == true {
Text("\(txt.text)")
.bold()
.italic()
.frame(width: txt.width, height: txt.height, alignment: txt.alignment)
.font(txt.textfont)
.foregroundColor(txt.textcolor)
.background(txt.textbackcolor)
.cornerRadius(txt.corner)
}
}
关于数据
@State private var testvar = [
Tests(id: 000, text: "Hello World 1", textcolor: .red, textfont: .title, height: 100, width: 300, alignment: .trailing, textbackcolor: Color(.blue).opacity(0.2), corner: 20, show: true, user: "System"),
Tests(id: 001, text: "Hello World 2", textcolor: .green, textfont: .largeTitle, height: 300, width: 300, alignment: .center, textbackcolor: .black, corner: 50, show: true, user: "George"),
]
我有
如果您想知道为什么 .largeTitle 不是斜体,请参阅 How to apply .italic() to .largeTitle Font?
更新以反映讨论
为了能够根据数组中的值修改样式,请使用简单的文本扩展
extension Text {
func style(bold: Bool = false, italic: Bool = false) -> Self {
var s = self
if bold {
s = s.bold()
}
if italic {
s = s.italic()
}
return s
}
}
这是(我希望如此)不言自明