我遇到一些SwiftUI的小部件代码问题。我想创建一个Text组件,将文本按比例缩小,但仅在其中包含的文本不合适的情况下。但是我看到它一直在缩小文本的比例。有人知道我在做什么错吗?
Text(verbatim: entry.records[0].value)
.foregroundColor(.white)
.font(Font.custom("SFCompactDisplay-Thin", size: 48))
.minimumScaleFactor(0.75)
.frame(minWidth: 100, idealWidth: 200, maxWidth: .infinity, minHeight: 48, idealHeight: 48, maxHeight: 48, alignment: .leading)
.padding(.top, 0)
.padding(.bottom, 12)
.padding(.leading, 10)
答案 0 :(得分:0)
运行代码后,看起来运行正常。 (图1,图2)
如果仍有问题,请删除maxWidth: .infinity,
并进行查看。 (图3)
请让我知道最终结果是什么。 这是我的测试代码。
struct ContentView: View {
var body: some View {
Text(verbatim: "Hello world Hello world Hello world Hello world Hello world")
.foregroundColor(.black)
.font(Font.custom("SFCompactDisplay-Thin", size: 48))
.minimumScaleFactor(0.5)
.frame(minWidth: 100, idealWidth: 200, minHeight: 48, idealHeight: 48, maxHeight: 48, alignment: .leading)
.padding(.top, 0)
.padding(.bottom, 12)
.padding(.leading, 10)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
更新#1
HStack {
Text(verbatim: "Hello")
.foregroundColor(.black)
.font(Font.custom("SFCompactDisplay-Thin", size: 48))
.minimumScaleFactor(0.1)
.frame(minWidth: 10, idealWidth: 120, maxWidth: 120, minHeight: 48, idealHeight: 48, maxHeight: 48, alignment: .leading)
.padding(.top, 0)
.padding(.bottom, 12)
.padding(.leading, 10)
Spacer()
}
答案 1 :(得分:0)
您的影响归因于.frame
的限制。在您不希望文本缩放的情况下,请不要限制其边框,例如
Text(verbatim: "World")
.foregroundColor(.white)
.font(Font.custom("SFCompactDisplay-Thin", size: 48))
.minimumScaleFactor(0.75)
.frame(maxWidth: .infinity, alignment: .leading) // << here !!
注意:48种字体大小不等于屏幕上的48点。