SwiftUI 多行文本总是在 1 行截断
SwiftUI multiline text always truncating at 1 line
我正在尝试创建一个选项列表供用户选择。调试预览显示了一般的外观和感觉。我的问题是在 MultipleChoiceOption
中将 nil
传递给 .lineLimit
不允许文本超过 1 行。我该如何纠正?
struct Card<Content: View> : View {
private let content: () -> Content
init(content: @escaping () -> Content) {
self.content = content
}
private let shadowColor = Color(red: 69 / 255, green: 81 / 255, blue: 84 / 255, opacity: 0.1)
var body: some View {
ZStack {
self.content()
.padding()
.background(RoundedRectangle(cornerRadius: 22, style: .continuous)
.foregroundColor(.white)
.shadow(color: shadowColor, radius: 10, x: 0, y: 5)
)
}
.aspectRatio(0.544, contentMode: .fit)
.padding()
}
}
struct MultipleChoiceOption : View {
var option: String
@State var isSelected: Bool
var body: some View {
ZStack {
Rectangle()
.foregroundColor(self.isSelected ? .gray : .white)
.cornerRadius(6)
.border(Color.gray, width: 1, cornerRadius: 6)
Text(self.option)
.font(.body)
.foregroundColor(self.isSelected ? .white : .black)
.padding()
.multilineTextAlignment(.leading)
.lineLimit(nil)
}
}
}
struct MultipleChoice : View {
@State var selectedIndex = 1
var options: [String] = [
"Hello World",
"How are you?",
"This is a longer test This is a longer test This is a longer test This is a longer test This is a longer test This is a longer test"
]
var body: some View {
GeometryReader { geometry in
ScrollView {
VStack(alignment: .leading, spacing: 12) {
ForEach(self.options.indices) { i in
MultipleChoiceOption(option: self.options[i],
isSelected: i == self.selectedIndex)
.tapAction { self.selectedIndex = i }
}
}
.frame(width: geometry.size.width)
}
}
.padding()
}
}
struct MultipleChoiceCard : View {
var question: String = "Is this a question?"
var body: some View {
Card {
VStack(spacing: 30) {
Text(self.question)
MultipleChoice()
}
}
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
// NavigationView {
VStack {
MultipleChoiceCard()
Button(action: {
}) {
Text("Next")
.padding()
.foregroundColor(.white)
.background(Color.orange)
.cornerRadius(10)
}
}
.padding()
// .navigationBarTitle(Text("Hello"))
// }
}
}
#endif
SwiftUI 目前存在一个错误,导致 nil lineLimit 无法工作。
如果您现在必须解决这个问题,您可以包装一个 UITextField:
https://icalvin.dev/post/403
我遇到了同样的问题并使用了这个解决方法:
添加修饰符:
.frame(idealHeight: .infinity)
请查看 Xcode 11 GM 的回答:
总结:添加 .fixedSize(horizontal: false, vertical: true)
— 这在我的用例中对我有用。
修饰符 fixedSize()
防止截断多行文本。
HStack 内部
Text("text").fixedSize(horizontal: false, vertical: true)
内部 VStack
Text("text").fixedSize(horizontal: true, vertical: false)
我正在尝试创建一个选项列表供用户选择。调试预览显示了一般的外观和感觉。我的问题是在 MultipleChoiceOption
中将 nil
传递给 .lineLimit
不允许文本超过 1 行。我该如何纠正?
struct Card<Content: View> : View {
private let content: () -> Content
init(content: @escaping () -> Content) {
self.content = content
}
private let shadowColor = Color(red: 69 / 255, green: 81 / 255, blue: 84 / 255, opacity: 0.1)
var body: some View {
ZStack {
self.content()
.padding()
.background(RoundedRectangle(cornerRadius: 22, style: .continuous)
.foregroundColor(.white)
.shadow(color: shadowColor, radius: 10, x: 0, y: 5)
)
}
.aspectRatio(0.544, contentMode: .fit)
.padding()
}
}
struct MultipleChoiceOption : View {
var option: String
@State var isSelected: Bool
var body: some View {
ZStack {
Rectangle()
.foregroundColor(self.isSelected ? .gray : .white)
.cornerRadius(6)
.border(Color.gray, width: 1, cornerRadius: 6)
Text(self.option)
.font(.body)
.foregroundColor(self.isSelected ? .white : .black)
.padding()
.multilineTextAlignment(.leading)
.lineLimit(nil)
}
}
}
struct MultipleChoice : View {
@State var selectedIndex = 1
var options: [String] = [
"Hello World",
"How are you?",
"This is a longer test This is a longer test This is a longer test This is a longer test This is a longer test This is a longer test"
]
var body: some View {
GeometryReader { geometry in
ScrollView {
VStack(alignment: .leading, spacing: 12) {
ForEach(self.options.indices) { i in
MultipleChoiceOption(option: self.options[i],
isSelected: i == self.selectedIndex)
.tapAction { self.selectedIndex = i }
}
}
.frame(width: geometry.size.width)
}
}
.padding()
}
}
struct MultipleChoiceCard : View {
var question: String = "Is this a question?"
var body: some View {
Card {
VStack(spacing: 30) {
Text(self.question)
MultipleChoice()
}
}
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
// NavigationView {
VStack {
MultipleChoiceCard()
Button(action: {
}) {
Text("Next")
.padding()
.foregroundColor(.white)
.background(Color.orange)
.cornerRadius(10)
}
}
.padding()
// .navigationBarTitle(Text("Hello"))
// }
}
}
#endif
SwiftUI 目前存在一个错误,导致 nil lineLimit 无法工作。
如果您现在必须解决这个问题,您可以包装一个 UITextField: https://icalvin.dev/post/403
我遇到了同样的问题并使用了这个解决方法:
添加修饰符:
.frame(idealHeight: .infinity)
请查看 Xcode 11 GM 的回答:
总结:添加 .fixedSize(horizontal: false, vertical: true)
— 这在我的用例中对我有用。
修饰符 fixedSize()
防止截断多行文本。
HStack 内部
Text("text").fixedSize(horizontal: false, vertical: true)
内部 VStack
Text("text").fixedSize(horizontal: true, vertical: false)