![](/img/trans.png)
[英]How to make a collectionView with resizable cells horizontally in swift
[英]How to make resizable text in an item in a list in Swift UI
我正在创建一个单元格,其中包含多个文本和一个图像。 我想要一个文本调整自身及其单元格的大小,但怎么做?
我尝试使用 .fixedSize() 和一些 UILabel 计算,但我可以修复它。
导航按钮也是列表中的一个单元格:
NavigationButton(destination: ..., isDetail: true, label: {
Cell(model: rowModel)
})
struct Cell: View {
var model: OfferModel
var body: some View {
VStack {
HStack {
Image(systemName: "camera").frame(width: 60, height: 60)
VStack(alignment: .leading) {
Text(model.name)
.font(.caption)
.textContentType(.name)
Text(self.model.text)
.lineLimit(3)
.multilineTextAlignment(.leading)
.font(.title)
}
Spacer()
OfferPriceView(model: model, alignment: .trailing)
}
OfferDetailInformationView(key: "Street", value: model.adress, style: .streetAddressLine1)
OfferDetailInformationView(key: "City", value: model.city, style: .addressCity)
OfferDetailInformationView(key: "Country", value: model.country, style: .countryName)
}
}
}
struct OfferDetailInformationView: View {
var key: String
var value: String
var style: UITextContentType?
init(key: String, value: String, style: UITextContentType? = nil) {
self.key = key
self.value = value
self.style = style
}
var body: some View {
HStack {
Text("\(key):").bold()
Text(value)
.textContentType(style)
Spacer()
}
}
}
struct OfferPriceView : View {
let model: OfferModel
let alignment: HorizontalAlignment
init(model: OfferModel = .example, alignment: HorizontalAlignment = .center) {
self.model = model
self.alignment = alignment
}
var body: some View {
VStack(alignment: alignment) {
Text("\(String(format: "%.2f", model.cost))\(model.currency)").bold().color(.red)
Text(model.paymentRate).color(.red)
}
}
}
如果文本不适合一行,我希望带有 model.text 的文本垂直扩展。 现在它只是将 tex 剪成一行。
视图结构如下:
尝试
Text("Hello")
.font(.largeTitle)
.minimumScaleFactor(0.3)
.lineLimit
修饰符是根据其内容控制Text
高度的修饰符。
所以如果你想要无限行的文本(应该适合屏幕或者你必须使用ScrollView
),你应该使用这个:
.lineLimit(nil)
更新
在 Xcode 11 Beta 7 中测试, nil
按预期工作,没有限制。 但是如果nil
是不可接受的(就像它不是在早期版本中一样),那么将它设置为一个大整数:
.lineLimit(Int.max)
请记住,在SwiftUI
达到第一个发布版本之前,第二个选项只是一种解决方法。
您可以使用 lineLimit(nil) 使文本多行
Text(repo.description)
.font(.subheadline)
.lineLimit(nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.