gpt4 book ai didi

SwiftUI:防止长词断字

转载 作者:行者123 更新时间:2023-12-05 04:51:49 25 4
gpt4 key购买 nike

如何防止长词断字,例如调整字体大小以适应宽度?

要求:外部灰色框必须具有相同的固定大小。应该是 2 行。

var body: some View {
VStack {
Spacer()

Image(systemName: "square")

.resizable()
.frame(width: 50, height: 50)

Spacer()
Text("Acknowledgement for the rest")
.allowsTightening(true)
.minimumScaleFactor(0.01)
.lineLimit(2)
.multilineTextAlignment(.center)

}
.padding()
.frame(width: 140, height: 150, alignment: .center)
.background(
ZStack {
RoundedRectangle(cornerRadius: 10)
.foregroundColor(Color(hex: "f9f9f9"))
}
)

enter image description here

最佳答案

使用fixedSize 修饰符:


enter image description here


import SwiftUI

struct ContentView: View {

var body: some View {
VStack {
Spacer()

Image(systemName: "square")
.resizable()
.frame(width: 50, height: 50)

Spacer()
Text("Acknowledgement for the rest")
.fixedSize() // <<: Here
.allowsTightening(true)
.minimumScaleFactor(0.01)
.lineLimit(nil)
.multilineTextAlignment(.center)

}
.padding()
.frame(height: 150)
.background(
ZStack {
RoundedRectangle(cornerRadius: 10)
.foregroundColor(Color.blue)
}
)
}

}

关于SwiftUI:防止长词断字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66776020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com