问题
I am seeing some odd Text field behavior on my device that I did not see in the simulator. I have a standard grouping of text fields within a VStack that call from an @ObservedObject variable, like follows:
@ObservedObject var timer: ActivityTimer
var body: some View {
VStack {
VStack {
Text(timer.currentCountdownString).font(Font.system(size: 90))
Text(timer.currentActivityName).font(.largeTitle).bold().underline().padding(.bottom, 5)
Text(timer.currentIncline).font(.title)
}
.padding()
.cornerRadius(10)
}
When the variable changes I see the text field change on the device but every so often the output is truncated into ..., please see below. Thank you in advance for your assistance.
回答1:
I ran into this issue on rotation to landscape (even though there was more horizontal room.) The way I fixed it was to call .fixedSize()
on the Text
.
Text(timer.currentActivityName)
.font(.largeTitle)
.bold()
.underline()
.fixedSize()
.padding(.bottom, 5)
Make sure to do your fixedSize call after setting weight, font, etc. but before your padding.
回答2:
Could be a bug as mentioned in your post comment, but for the time being, you could wrap the VStacks in a GeometryReader and then set the width of the frames for all three Text objects to be the width of the geometry and set the alignment of your VStack to center as such:
@ObservedObject var timer: ActivityTimer
var body: some View {
GeometryReader { geom in
VStack {
VStack(alignment: .center) {
Text(timer.currentCountdownString).font(Font.system(size: 90))
Text(timer.currentActivityName).font(.largeTitle).bold().underline().padding(.bottom, 5)
Text(timer.currentIncline).font(.title)
}
.frame(width: geom.size.width)
.padding()
.cornerRadius(10)
}
}
来源:https://stackoverflow.com/questions/59375064/swiftui-text-behavior