Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make subscripts and superscripts using NSAttributedString?

I need to make subscripts for chemistry formulas (H2O, Na^2+, etc)?

Is this possible to do with NSAttributedString, or is there an alternative/easier way to make subscripts?

like image 699
Mahir Avatar asked Dec 18 '11 23:12

Mahir


People also ask

How do you type subscript and superscript?

Insert a superscript or subscript symbolOn the Insert tab, click Symbol. In the Symbol box, in the Font drop-down list, select (normal text) if it isn't already selected. In the Symbol box, in the Subset drop-down list, select Superscripts and Subscripts.

What is superscript and subscript with example?

Superscripts are characters set above the normal line of type (e.g., in 2ⁿᵈ) and subscripts are characters set below (e.g., in Cᵥₑₓ).

How do you represent a superscript?

Use "^" for superscripts: 2^6, e^3, etc. ("**" instead of "^" is also OK.) Use parentheses if either the base or the exponent contains more than one mathematical symbol.


2 Answers

Here's what I did in iOS 6. First add the CoreText, and QuartzCore frameworks. Then import:

#import <QuartzCore/QuartzCore.h> #import <CoreText/CTStringAttributes.h> #import <CoreText/CoreText.h> 

I made a small function that inputs a plain NSString and exports a NSMutableAttributedString with the last character in superscript. This can be modified to allow setting superscript or subscript, change kCTSuperscriptAttributeName value to -1. Also you could add a variable to specify where to put the superscript in the string. Right now it just assumes the end of the string.

- (NSMutableAttributedString *)plainStringToAttributedUnits:(NSString *)string; {     NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];     UIFont *font = [UIFont systemFontOfSize:10.0f];     UIFont *smallFont = [UIFont systemFontOfSize:9.0f];      [attString beginEditing];     [attString addAttribute:NSFontAttributeName value:(font) range:NSMakeRange(0, string.length - 2)];     [attString addAttribute:NSFontAttributeName value:(smallFont) range:NSMakeRange(string.length - 1, 1)];     [attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:NSMakeRange(string.length - 1, 1)];     [attString addAttribute:(NSString*)kCTForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, string.length - 1)];     [attString endEditing];     return attString; } 

Now when I want to use it I can do the following to put it in a UITextField:

    NSString *qlwUnitsPlainText = @"m3";     self.quantityLoadWeightUnits_textField.attributedText = [self plainStringToAttributedUnits:qlwUnitsPlainText]; 

I hope this helps somebody else, there's not many examples out there!

like image 198
Robert Wasmann Avatar answered Sep 18 '22 12:09

Robert Wasmann


This is possible to do with NSAttributedString. The attribute constant you're looking for depends on your platform. For Mac OS X it is NSSuperscriptAttributeName and on iOS it is kCTSuperscriptAttributeName. Pass in a negative value for subscript.

The only caveat is that UILabel on iOS can't draw NSAttributedStrings (yet, fingers crossed for iOS 6). You would need to draw the text using Core Text or find some third party replacement for UILabel that can draw an NSAttributedString.

like image 44
Mark Adams Avatar answered Sep 21 '22 12:09

Mark Adams