WatchOS ScrollView 无法正确换行文本
WatchOS ScrollView Doesn't Wrap Text Properly
现在,如果我设置一个具有所需宽度和高度的框架,我可以从 'articles' 中看到我想要的文本。
如果高度足够长,它将显示文章的全部内容 'body',但还有大量的 space。我希望框架可以根据我拥有的文本大小调整其大小,以便滚动视图可以根据每篇文章正文所需的文本框架正确滚动。
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
滚动时正常工作?我错过了什么吗?
非常感谢您的宝贵时间。
将 .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
}
}
}
}
}
现在,如果我设置一个具有所需宽度和高度的框架,我可以从 'articles' 中看到我想要的文本。
如果高度足够长,它将显示文章的全部内容 'body',但还有大量的 space。我希望框架可以根据我拥有的文本大小调整其大小,以便滚动视图可以根据每篇文章正文所需的文本框架正确滚动。
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
滚动时正常工作?我错过了什么吗?
非常感谢您的宝贵时间。
将 .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
}
}
}
}
}