How do you set a currency label to show smaller font size for cents?

本秂侑毒 提交于 2020-12-15 04:56:35

问题


I have a list of labels with dollar amounts (example: $1,526.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


回答1:


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))
}


来源:https://stackoverflow.com/questions/62886084/how-do-you-set-a-currency-label-to-show-smaller-font-size-for-cents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!