组合 n 个文本对象
Combine n number of Text objects
在 SwiftUI
中,您可以像这样组合 Text
个对象:
var body: some View {
Text("You can") + Text(" add lots of objects that wrap normally")
}
这带来了将多个文本对象作为一个对象的好处,这意味着它们位于同一行并适当换行。
我想不通的是如何根据数组或任何递增方式组合 n 个 Text
对象。
我试过ForEach
这样,
var texts = ["You can", " add lots of objects that wrap normally"]
var body: some View {
ForEach(texts.identified(by: \.self)) {
Text([=11=])
}
}
但是看起来像这样
当我希望它看起来像这样时
谁能告诉我这是怎么做到的?
用例:为文本对象的一部分而不是其他部分设置样式。就像 NSMutableAttributedString
的可能性一样
您似乎在描述这样的事情:
var body: some View {
let strings = ["manny ", "moe", " jack"]
var texts = strings.map{Text([=10=])}
texts[1] = texts[1].underline() // illustrating your use case
return texts[1...].reduce(texts[0], +)
}
但是,我认为最好等到属性字符串到达。
在 SwiftUI
中,您可以像这样组合 Text
个对象:
var body: some View {
Text("You can") + Text(" add lots of objects that wrap normally")
}
这带来了将多个文本对象作为一个对象的好处,这意味着它们位于同一行并适当换行。
我想不通的是如何根据数组或任何递增方式组合 n 个 Text
对象。
我试过ForEach
这样,
var texts = ["You can", " add lots of objects that wrap normally"]
var body: some View {
ForEach(texts.identified(by: \.self)) {
Text([=11=])
}
}
但是看起来像这样
当我希望它看起来像这样时
谁能告诉我这是怎么做到的?
用例:为文本对象的一部分而不是其他部分设置样式。就像 NSMutableAttributedString
您似乎在描述这样的事情:
var body: some View {
let strings = ["manny ", "moe", " jack"]
var texts = strings.map{Text([=10=])}
texts[1] = texts[1].underline() // illustrating your use case
return texts[1...].reduce(texts[0], +)
}
但是,我认为最好等到属性字符串到达。