是否在Swiftui的Text()中自动调整字体大小?
是否有根据要显示的文本长度自动调整字体大小的方法?例如,通过设置帧大小。它能否自动减小字体大小,使显示字体的宽度保持不变是否在Swiftui的Text()中自动调整字体大小?,swiftui,Swiftui,是否有根据要显示的文本长度自动调整字体大小的方法?例如,通过设置帧大小。它能否自动减小字体大小,使显示字体的宽度保持不变 var body: some View { VStack { ZStack { GeometryReader { g in ZStack { ScrollView(.vertical, showsIndic
var body: some View {
VStack {
ZStack {
GeometryReader { g in
ZStack {
ScrollView(.vertical, showsIndicators: true, content: {
VStack {
ZStack {
VStack {
Image("cat")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width:g.size.width, height:g.size.height/4*3)
.clipped(antialiased:true)
.shadow(radius:25, x:0, y:0)
VStack (){
HStack {
VStack (alignment: .leading) {
HStack {
Text("XXXX").font(.title)
.font(.system(size:35)).foregroundColor(.black).shadow(radius: 5)
})
Spacer()
}
HStack {
Text("xxxxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxx ").font(.title2) // very long text here
Spacer()
}
}
}
}
.padding()
.frame(width:g.size.width, height:g.size.height/4*1, alignment: .top)
}
}.frame(width: g.size.width, height: g.size.height, alignment: .top)
}
.edgesIgnoringSafeArea(.all)
}
}
}
正如在我的代码中看到的,那里有一个很长的文本。除iPod Touch外,该文本可以在大多数设备上完美显示,因为太长而无法显示,它会导致后面出现“xxxxxxxxxxxxxxxxxxxx…”、“…”。它是否自动跳转到下一行,或者缩小字体以便后面不会出现“…”字样?使用
最小缩放因子(quo;:)
struct ContentView:View{
var body:一些观点{
VStack{
文本(“测试字符串”)
.minimumScaleFactor(0.1)//我已经包含了我的代码并再次澄清了我的问题。@user6539552是使用此.minimumScaleFactor(0.1)在字体之后。它会自动变为两行并改变字体大小。如果你给出高度,那么它会根据帧自动调整。我检查了你的代码,然后用这个来固定。我可以知道为什么我们选择系数为0.1吗?这是一个最小比例,最终会降低字体大小。你可以根据你的要求来设置。我选择v对于这个例子来说,框架的大小非常小。我可以强制它变成多行而不是收缩文本吗?
struct ContentView: View {
var body: some View {
VStack{
Text("Test string")
.minimumScaleFactor(0.1) //<--Here
.frame(width: 15, height: 15)
}
}
}