I need to display the following equation in UILabel. But i don't know how to type the superscript text in xCode.
2xˆ3-3xˆ2-36x+2
For reference i use ^ symbol for indicate the superscript.
I need to display the following equation in UILabel. But i don't know how to type the superscript text in xCode.
2xˆ3-3xˆ2-36x+2
For reference i use ^ symbol for indicate the superscript.
Pressing Cmd+Ctrl+Space will open a special characters menu. Check if the "Digits — All" category is in the left-hand column. If it isn't, click the gear icon, then select this category — add it to the list.
It can be achieved using NSAttributedString. Following may be of some help:
How to use NSSuperscriptAttributeName for OS X
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
let yourFont = UIFont.systemFont(ofSize: 20, weight: .medium)
"2x^{3}-3x^{2}-36x+2".superscripted(font: yourFont)
The last line will return an NSAttributedString
which will look exactly like what you're looking for. Just assign it to a UILabel
s 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.