问题
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!
回答1:
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.
回答2:
Add CoreText framework, import CoreText.h, and use UIlabel.attributedText - it has full support for NSAttributedString. Asked and answered repeatedly on SO already.
回答3:
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];
来源:https://stackoverflow.com/questions/10512255/superscript-characters-in-string-ios