我正在 SwiftUI 中开发一个代表硬币的视图。硬币是一个按钮。
在硬币内部,我想将代表金额的文本居中,例如,如果文本是“0.01”代表 1 美分。
问题是,如果文本大小为 30,我无法使文本在按钮内居中。这是我的尝试,将填充设置为.all。
Button(action: addToTotal)
{
Text(coinAmount.toTwoDecimalOrTruncatedString())
.font(.system(size: 30))
.fontWeight(.bold)
.multilineTextAlignment(.center)
.lineLimit(0)
}
.padding(.all)
.accentColor(.white)
.frame(width: 100, height: 100, alignment: .bottom)
.background(Color.orange)
.cornerRadius(100)
Run Code Online (Sandbox Code Playgroud)
cointAmount是一个Float.
toTwoDecimalOrTruncatedString是一种扩展方法,如果数字是浮点数,则将硬币金额截断为两位小数字符串。
我认为问题是由于填充造成的,但我无法将文本像素完美地集中在按钮视图内:
有什么建议 ?
.frame只需在您的ie中使用中心对齐
}
.padding(.all)
.accentColor(.white)
.frame(width: 100, height: 100, alignment: .center) // << here!
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1493 次 |
| 最近记录: |