swift - swiftUI中如何防止文本被截断
问题描述
我想打印出标题,以免被截断。
VStack(alignment: .leading, spacing: 5) {
Text(fruit.title)
.font(.title2)
.fontWeight(.bold)
Text(fruit.headline)
.font(.caption)
.foregroundColor(Color.secondary)
}
我试图用固定大小来解决这个问题,但是对齐方式被扭曲了
有没有办法解决?
解决方案
您可能需要.frame(maxWidth: .infinity, alignment: .leading)
在VStack
. 这使得 VStack 占用尽可能多的水平空间,然后将其内容 (Text(fruit.title)
和Text(fruit.headline)
) 向左对齐。
struct Fruit: Hashable {
var title: String
var headline: String
}
struct ContentView: View {
let fruits = [
Fruit(title: "Pomegranate", headline: "Sweet, bell-shaped fruits that have been enjoyed since ancient times. They can be eaten crisp or soft."),
Fruit(title: "Plum", headline: "Plums are a very nutritious fruit. An excellent source of vitamins, minerals, fiber and antioxidants."),
Fruit(title: "Apple", headline: "An apple"),
]
var body: some View {
VStack {
ForEach(fruits, id: \.self) { fruit in
HStack {
Image(fruit.title)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 80, height: 80)
VStack(alignment: .leading, spacing: 5) {
Text(fruit.title)
.font(.title2)
.fontWeight(.bold)
Text(fruit.headline)
.font(.caption)
.foregroundColor(Color.secondary)
}
.frame(maxWidth: .infinity, alignment: .leading) /// here!
}
}
}
}
}
结果:
推荐阅读
- python - MacOS:如何使用 docker-compose.yml 和 Dockerfile 挂载卷?
- deep-learning - 检查目标时出错:预期输出的形状为 (10,) 但得到的数组形状为 (1,)
- java - 硒 | 元素不可交互错误:探索了堆栈溢出的所有选项
- algorithm - 在循环调度算法中,当任务被拆分为多个时间量时,如何保留任务的当前状态?
- android-studio - `flutter packages get` 和 `flutter pub get` 有什么区别?
- python - 如何在 tkinter 中将对象保持在其他对象之上
- python - python中的数据框创建问题
- netlogo - 如何在 Netlogo 中更改浅色品种的颜色
- algorithm - 高效的分治算法
- html - 如何将导航与最多 3 个元素(列表)连续居中 [响应式]