SwiftUI-文本溢出屏幕

pftdvrlh  于 2022-11-28  发布在  Swift
关注(0)|答案(1)|浏览(233)

我正在尝试将一长段文字放进屏幕,但它一直溢出。有没有办法将文字换行?
我试着使用对齐使它居中,但它仍然离开屏幕。

import SwiftUI

struct OnboardingPage3: View {
    var body: some View {
        ZStack {
            Color("Onboarding")
                .edgesIgnoringSafeArea(.all)
            VStack {
                Color("Onboarding")
                    .edgesIgnoringSafeArea(.all)
                Image("HomeScreen")
                    .resizable()
                    .frame(width: 300, height: 600)
                    .padding(EdgeInsets(top: 0, leading: 0, bottom: 200, trailing: 0))
                
                Text("This is your home screen where you can see how much progress you have made throughout the day as well as a streaks bar to keep track of how many days straight you have been exercising.")
                    .frame(alignment: .center)
            }
        }
    }
}
struct OnboardingPage3_Previews: PreviewProvider {
    static var previews: some View {
        OnboardingPage3()
    }
}
yrdbyhpb

yrdbyhpb1#

您的文本实际上并没有溢出,只是被截断了。

为了防止这种情况,您可以使用fixedSize(horizontal:vertical:)修饰符。我还对您的代码做了一些其他的编辑-没有必要使用这么多的.edgesIgnoringSafeArea,并且ZStack可能会对定位产生一些意想不到的副作用。

struct OnboardingPage3: View {
    var body: some View {
        VStack {
            Image("HomeScreen")
                .resizable()
                .aspectRatio(contentMode: .fit) /// use this to maintain the aspect ratio
                .frame(width: 200) /// now you only need to supply 1 dimension

            Text("This is your home screen where you can see how much progress you have made throughout the day as well as a streaks bar to keep track of how many days straight you have been exercising.")
                .fixedSize(horizontal: false, vertical: true)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .padding(20)
        .background(
            Color.gray
                .edgesIgnoringSafeArea(.all)
        )
    }
}

结果:

相关问题