[英]SwiftUI - Issue with Text not filling out first line
如何防止我的文本自动显示在两行上,而不是先将第一行填写到最后。 这是一个错误吗? 任何建议都非常感谢。
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
HStack(spacing: 18) {
VStack {
Text("Need to add a complete sentence.")
.font(.subheadline)
.frame(maxWidth: .infinity, alignment: .leading)
}
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.green)
VStack {
Text("Need to add a complete sentence.")
.font(.subheadline)
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
.background(Color.gray)
}
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.yellow)
.padding()
}.edgesIgnoringSafeArea(.all)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
这是 iOS 的预期“孤立词”行为。
所以我会这样离开它并对结果感到满意。 但如果你真的想阻止这种情况,恐怕SwiftUI
中唯一的解决方案是使用带有UIViewRepresentable
的UILabel
并设置lineBreakStrategy = []
如果您搜索swift orphaned words
,您可以了解有关该主题的更多详细信息。
https://medium.com/@pearsontsp/working-with-orphaned-words-in-ios-391a4a928e48
我不推荐,但也有一个 hacky 解决方案。 只需在文本中添加一些额外的空格即可。 所以 swift 认为“句子”这个词不再是孤儿:
Text("Need to add a complete sentence. ")
那么结果将是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.