Daniel
Daniel

Reputation: 1295

superscript characters in string IOS

My app in xcode has a tableview with some product data. One of my products have a superscript e in its name. How can I use superscript characters in a string like: texte

I can get it to work with numbers: text\u2070 -> text0 or text\u2071 -> text1. But how to do this with other characters?

thx!

Upvotes: 1

Views: 9732

Answers (3)

Bhushan
Bhushan

Reputation: 175

For SubScript use value for kCTSuperscriptAttributeName as @-1.

As per the document

@discussion Value must be a CFNumberRef. Default is int value 0. If supported by the specified font, a value of 1 enables superscripting and a value of -1 enables subscripting.

extern const CFStringRef kCTSuperscriptAttributeName CT_AVAILABLE(10_5, 3_2);

Example- [lblHeader setText:@“Headers [Alpha1 – text”];

        NSMutableAttributedString *headerSubscript = [[NSMutableAttributedString alloc]initWithAttributedString: lblHeader.attributedText];

        [headerSubscript addAttribute:(NSString *)kCTSuperscriptAttributeName value:@-1 range:NSMakeRange(14,1)];      

        [lblHeader setAttributedText:headerSubscript];

Upvotes: 2

Adam
Adam

Reputation: 33146

Add CoreText framework, import CoreText.h, and use UIlabel.attributedText - it has full support for NSAttributedString. Asked and answered repeatedly on SO already.

Upvotes: 2

Jason
Jason

Reputation: 4322

tl;dr: NSString does not support concept of super/sub script. That's more or less a UI formatting concern.

One possible solution is to dynically add UILabels in code instead of interface builder. You can add a second UILabel with a smaller font size.

Upvotes: 2

Related Questions