SwiftUI Text Behavior

…衆ロ難τιáo~ 提交于 2021-02-11 13:56:16

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!