swift - 在不增加堆栈宽度的情况下更改文本宽度
问题描述
let months = ["january" , "februarry", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]
var body: some View {
HStack {
ForEach(months, id: \.self) { month in
VStack {
if month == "october" {
Text(month)
}
Rectangle()
.frame(width: 25, height: 10)
}
}
}
}
如何在不更改 VStack 的宽度以及方块之间的距离的情况下以全长显示文本。
它应该如下所示:
解决方案
好的,那么这个 hack 怎么样:
let months = ["january" , "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]
@State var chosenMonth = ""
var body: some View {
VStack {
HStack {
Spacer()
Text(chosenMonth)
Spacer()
}
HStack {
ForEach(self.months, id: \.self) { month in
Rectangle().frame(width: 25, height: 10).onAppear(perform: {
if month == "october" {
self.chosenMonth = month
}
})
}
}
}
}
推荐阅读
- php - PHP 致命错误:未捕获错误:调用未定义函数 posix_getgrgid()
- windows - (Windows) `dartdoc` 在运行我的(小)包时为 Dart 和 Flutter 的 SDK 中的每个基础库生成文档文件
- javascript - TypeError:this.props.AddToList 不是函数
- html - Safari 替换粘性表格标题
- python - Google Colab 中的 WolframLanguageSession 在哪里?
- sql - 选择值最近更改的行
- android - 使用错误的 google-service.json 更新应用后通知中断
- r - 绘图坐标与 *jpg 图像坐标不匹配
- javascript - 这种指令的解释是什么 let {max}=Math;
- amazon-web-services - 使用过滤器订阅主题的 aws cli 命令