如何设置货币标签以显示较小的美分字体?
How do you set a currency label to show smaller font size for cents?
我有一个带有美元金额的标签列表(例如:1,526.69 美元)。
如何将美分(在本例中为 69 美分)的字体大小设置为小于整美元?
喜欢下图
您可以在标签中使用可变属性字符串。您需要找到字符串中美分的范围,设置较小的字体大小并增加向美分添加基线偏移量:
let mutableAttributedString = NSMutableAttributedString(string: ",526.69")
if let range = mutableAttributedString.string.range(of: #"(?<=.)(\d{2})$"#, options: .regularExpression) {
mutableAttributedString.setAttributes([.font: UIFont.systemFont(ofSize: 5),.baselineOffset: 5],
range: NSRange(range, in: mutableAttributedString.string))
}
我有一个带有美元金额的标签列表(例如:1,526.69 美元)。
如何将美分(在本例中为 69 美分)的字体大小设置为小于整美元?
喜欢下图
您可以在标签中使用可变属性字符串。您需要找到字符串中美分的范围,设置较小的字体大小并增加向美分添加基线偏移量:
let mutableAttributedString = NSMutableAttributedString(string: ",526.69")
if let range = mutableAttributedString.string.range(of: #"(?<=.)(\d{2})$"#, options: .regularExpression) {
mutableAttributedString.setAttributes([.font: UIFont.systemFont(ofSize: 5),.baselineOffset: 5],
range: NSRange(range, in: mutableAttributedString.string))
}