英文:
How do you add a view to the end of a multi-line Text view?
问题
我有一个Text
视图,它是动态的,有时可能是一个跨越2或3行的长名称。如何在多行Text
视图的最后一个单词旁边立即添加一个视图?
如何将Age (23)
立即移到"last-name"的右边?
HStack(alignment: .bottom) {
Text("First Middle hyphenated-long-last-name")
Text("Age (\(age))")
.font(.caption2)
}
英文:
I have a Text
view that is dynamic, and can sometimes be a long name that spans 2 or 3 lines. How can I add a view immediately next the last word of a multi-line Text
view?
How can I move Age (23)
immediately to the right of "last-name" ?
HStack(alignment: .bottom) {
Text("First Middle hyphenated-long-last-name")
Text("Age (\(age))")
.font(.caption2)
}
答案1
得分: 3
在Text视图之间添加加号(+)。这将连接Text视图,同时保留每个Text视图的独特格式。
var body: some View {
HStack(alignment: .bottom) {
Text("First Middle hyphenated-long-last-name") +
Text(" Age (\(age))")
.font(.caption2)
}
}
不同框架宽度的示例:
...并且在连接时不需要HStack或其他分组,除非需要额外的分组格式。
英文:
Add a plus(+) sign between the Text views. This will concatenate the Text views while keeping each Text view's unique formatting.
var body: some View {
HStack(alignment: .bottom) {
Text("First Middle hyphenated-long-last-name") +
Text(" Age (\(age))")
.font(.caption2)
}
}
Examples with different frame widths:
...and the HStack or other grouping is not needed when concatenating unless additional group formatting is needed.
答案2
得分: 0
如果您不介意年龄与姓名大小相同,您可以将它们放在同一个“Text”视图中:
HStack(alignment: .bottom) {
Text("姓名 年龄 (\(age))")
}
英文:
If you don’t mind the age being the same size as the name, you can just put them in the same Text
view:
HStack(alignment: .bottom) {
Text("First Middle hyphenated-long-last-name" + " Age (\(age))")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论