如何设置货币标签以显示较小的字体大小(以美分计)? [英] How do you set a currency label to show smaller font size for cents?

查看:83
本文介绍了如何设置货币标签以显示较小的字体大小(以美分计)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有美元金额的标签列表(例如:$ 1,526.69).

I have a list of labels with dollar amounts (example: $1,526.69).

如何将美分(在本例中为69美分)设置为小于整个美元的字体大小?

How do I set the font size for the cents (in this case, the 69 cents) to be smaller than the whole dollars?

就像下面的图片一样

Like the image below

推荐答案

您可以在标签中使用可变属性字符串.您将需要在字符串中找到美分的范围,并将其设置为较小的字体大小,并增加该范围,以为美分增加基线偏移量:

You can use a mutable attributed string in your label. You will need to find the range of the cents in your string set a smaller font size and increase add a baseline offset to the cents:

let mutableAttributedString = NSMutableAttributedString(string: "$1,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))
}

这篇关于如何设置货币标签以显示较小的字体大小(以美分计)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆