gpt4 book ai didi

ios - 组合 n 个文本对象

转载 作者:搜寻专家 更新时间:2023-11-01 06:25:12 25 4
gpt4 key购买 nike

SwiftUI 中,您可以像这样组合 Text 对象:

var body: some View {
Text("You can") + Text(" add lots of objects that wrap normally")
}

这带来了多个 Text 对象作为一个对象的好处,这意味着它们位于同一行并适当换行。

我想不通的是如何根据数组或任何递增方式组合 n 个 Text 对象。

我已经像这样尝试过ForEach

var texts = ["You can", " add lots of objects that wrap normally"]
var body: some View {
ForEach(texts.identified(by: \.self)) {
Text($0)
}
}

但是看起来像这样

enter image description here

当我想要它看起来像这样的时候

Manual Implementation

谁能告诉我这是怎么做到的?

用例:为文本对象的一部分而不是其他部分设置样式。就像 NSMutableAttributedString

的可能性一样

最佳答案

你似乎在描述这样的事情:

enter image description here

var body: some View {
let strings = ["manny ", "moe", " jack"]
var texts = strings.map{Text($0)}
texts[1] = texts[1].underline() // illustrating your use case
return texts[1...].reduce(texts[0], +)
}

但是,我认为最好等到属性字符串到达​​。

关于ios - 组合 n 个文本对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56919020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com