4

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.

Anbu Raj
  • 831
  • 2
  • 8
  • 25

3 Answers3

5

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.

See screenshot below

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Ranjith
  • 425
  • 6
  • 8
  • Nice trick....This answer solved another issue I had in the back of my mind related to math symbols without using a special math font.... – eharo2 May 20 '19 at 15:17
1

It can be achieved using NSAttributedString. Following may be of some help:
How to use NSSuperscriptAttributeName for OS X

Community
  • 1
  • 1
0

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 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.

Jeehut
  • 20,202
  • 8
  • 59
  • 80