Cemre Mengü
Cemre Mengü

Reputation: 18764

show formula with italic letter and superscript exponent in iOS

I am creating a calculator for iPhone (for practice) and I want to use the symbol x^2 without the ^ so that 2 appears directly above x. Is there a way to achieve this?

It would also be good to have an x symbol that is a bit "stylish" like the ones used in latex (e.g. such as this)

I am using iOS 5, XCode 4.2

Upvotes: 2

Views: 1001

Answers (1)

rob mayoff
rob mayoff

Reputation: 386018

I guess you mean you want to display x² in a label or button.

The easiest way is to use Unicode character U+00B2, SUPERSCRIPT TWO, which is what I did.

In Objective-C, you can put any 16-bit Unicode character in a string using a \u escape:

[self.squareButton setTitle:@"x\u00b2" forState:UIControlStateNormal];

If you want to set it in Interface Builder, you can use the Character Viewer to insert it.

Unicode also has character U+1D465 MATHEMATICAL ITALIC SMALL X. You can use \U0001D465 in a string to represent that character. Unfortunately, iOS doesn't seem to support displaying that character (in my test on the iOS 5.0 simulator).

So if you want the x to use an italic font but the 2 to use a regular font, you will need to use separate labels for the x and the 2, or use Core Text to render an attributed string containing both characters with appropriate attributes. As of iOS 5.1, no UIKit classes support drawing attributed strings.

Upvotes: 8

Related Questions