How to display superscript % character as string in UIlabel?

后端 未结 2 1188
别那么骄傲
别那么骄傲 2020-12-29 12:25

How to display superscript % character as string in UIlabel? I know % does not exist in unicode as a superscript but is there is any way we can display % as a superscript in

2条回答
  •  攒了一身酷
    2020-12-29 13:02

    For a simple to use Swift solution, you might want to checkout HandyUIKit. After importing it into your project (e.g. via Carthage – see instructions in README) you can do something like this:

    import HandyUIKit
    
    "57^{%}".superscripted(font: UIFont.systemFont(ofSize: 20, weight: .medium))
    

    This line will return an NSAttributedString which will look exactly like what you're looking for. Just assign it to a UILabels attributedText property and that's it!


    If you're looking for subscripting a text, simply use subscripted(font:) instead. It will recognize structures like CO_{2}. There's also superAndSubscripted(font:) if you want to combine both.

    See the docs for more information and additional examples.

提交回复
热议问题