现在,如果我将边框设置为所需的宽度和高度,则可以从“文章”中看到想要的文本。
如果高度足够长,它将显示所有文章“ body”,但具有大量空间。我想在框架可以根据我拥有的文本大小调整其大小的位置使用它,以便滚动视图可以根据每个文章正文所需的文本框架正确滚动。
import SwiftUI
let articles = [
Article (id: 0, title: "Trump as President", body: "Some very short string for the article at the moment."),
Article (id: 1, title: "Obama as President", body: "He may not have been the worst but was he every really the best? Could he have done more or less from what we know?"),
Article (id: 2, title: "Tanner Fry as President", body: "Who knows how well that would work as if Tanner Fry was the president. However we do know a little bit about him from his experience as a programmer. I mean who can just pick up different langauges just off the bat and start creating code for apps to run on their watch and/or phone? Not too many people know how to do that but you know who does? Tanner Fry does, that's right.")
]
var height = 0
struct ContentView: View {
var body: some View {
// Watch res = 448 - 368
ScrollView(.vertical) {
VStack(spacing: 10){
Text("Your News").font(.title)
ForEach(0..<articles.count) {index in
Text(articles[index].title)
Text(articles[index].body).frame(width: 170, height: 170)
// Text(articles[index].body).lineLimit(50).padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10))
// Height needs to be variable based on the amount of text in the
// articles description. OR find a wrapper
// We're talking about the frame for the body of text
}
}
}
}
}
如果frame
中article.body
的高度足够长,我可以滚动所有内容。否则,它将截断文本。是否有任何方法可以使高度随文本长度的变化而变化,以使watchOS在通过ScrollView
滚动时正常工作?我想念什么吗?
非常感谢您的宝贵时间。
答案 0 :(得分:1)
将.lineLimit(x)定义为希望文本扩展的最大行数。然后添加.fixedSize(horizontal:false,vertical:true)以确保大小不会因SwiftUI布局引擎而缩小。见下文。
struct ContentView: View {
var body: some View {
// Watch res = 448 - 368
ScrollView(.vertical) {
VStack(spacing: 10){
Text("Your News").font(.title)
ForEach(0..<articles.count) {index in
Text(articles[index].title)
Text(articles[index].body)
.lineLimit(nil)
.multilineTextAlignment(.leading)
.fixedSize(horizontal: false, vertical: true)
// Text(articles[index].body).lineLimit(50).padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10))
// Height needs to be variable based on the amount of text in the
// articles description. OR find a wrapper
// We're talking about the frame for the body of text
}
}
}
}
}