Text SwiftUI-文本最小缩放因子仅在需要时不缩放
我有一个文本,当它的字符串长度超出视图时,我想用Text SwiftUI-文本最小缩放因子仅在需要时不缩放,text,view,scale,swiftui,scaling,Text,View,Scale,Swiftui,Scaling,我有一个文本,当它的字符串长度超出视图时,我想用.minimumScaleFactor(0.1)修改它。但是,每次都会应用缩放,即使原始字体大小非常好 我的观点是这样的: VStack(alignment: .center, spacing: 0) { HStack { Image("medal \(place)").resizable() .foregroundColor(color
.minimumScaleFactor(0.1)
修改它。但是,每次都会应用缩放,即使原始字体大小非常好
我的观点是这样的:
VStack(alignment: .center, spacing: 0) {
HStack {
Image("medal \(place)").resizable()
.foregroundColor(color)
.frame(width: 40, height: 40)
Spacer()
Text(username)
.font(.bold(16))
.lineLimit(1)
.minimumScaleFactor(0.1)
.frame(alignment: .trailing)
.foregroundColor(Color("mediumTextColor"))
}
Spacer()
Text(score)
.font(.extraBold(60))
.foregroundColor(color)
.lineLimit(1)
.minimumScaleFactor(0.7)
Spacer()
}
.frame(height: 96)
.padding(10)
.cornerRadius(16)
.overlay(RoundedRectangle(cornerRadius: 16)
.stroke(color, lineWidth: 2))
在文本中添加
.layoutPriority(1)
修饰符,以确保它占用空间(如果有)。在我的两个文本视图所在的VStack周围添加GeometryReader解决了此问题。按照@gohnjanotis的要求:
GeometryReader { proxy in
VStack(alignment: .center, spacing: 0) {
HStack {
Image("medal \(self.place)").resizable()
.foregroundColor(self.color)
.frame(width: 40, height: 40)
Spacer()
Text(self.username)
.minimumScaleFactor(0.1)
.font(.bold(16))
.lineLimit(1)
.frame(alignment: .trailing)
.foregroundColor(Color("mediumTextColor"))
.layoutPriority(1)
}
.padding(.top, 10)
.padding(.horizontal, 10)
Spacer()
Text(self.score)
.font(.extraBold(60))
.foregroundColor(self.color)
.lineLimit(1)
.minimumScaleFactor(0.1)
.layoutPriority(1)
.padding(.horizontal, 10)
.padding(.bottom, 10)
.offset(y: -10)
}
.frame(width: proxy.size.width, height: proxy.size.height, alignment: .top)
}
.frame(maxHeight: 130)
在文本中添加.lineLimit(1)
,效果会很好
Xcode:11.3.1以上答案的延续 重要的是应用修改器的顺序:
Text("This is a really long sentence.")
.minimumScaleFactor(0.5)
.font(.custom("OpenSans", size: 15))
.lineLimit(1)
.layoutPriority(1)
不需要GeometryReader,除非您需要它来做其他事情重要的是,minimumScaleFactor()只在需要时才起作用,它是
.minimumScaleFactor
和.lineLimit(1)
的组合;否则它将无法工作<代码>布局优先级(1)不是这里的关键
例子——这很有效;比例仅在需要时应用:
Text("Something").minimumScaleFactor(0.5).lineLimit(1)
当我将整个东西嵌入GeometryReaderDid时,它实际上需要工作你能找到解决方案吗?我也面临同样的问题。你对一个回答说“当我把整个东西嵌入GeometryReader时,它实际上需要工作”-你能提供示例代码来说明你是如何解决的吗?我发布了我是如何解决这个问题的…我有3个
文本,在HStack`中嵌入了不同的Fonts,在每个文本上设置minimumScaleFactor
将是一件痛苦的事,但很高兴,在HStack
上设置minimumScaleFactor
和lineLimit
效果非常好,即使font
对每个文本设置不同。