NSAttributedString superscript styling
The following code seems to do the trick:
UIFont *fnt = [UIFont fontWithName:@"Helvetica" size:20.0];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"GGG®GGG"
attributes:@{NSFontAttributeName: [fnt fontWithSize:20]}];
[attributedString setAttributes:@{NSFontAttributeName : [fnt fontWithSize:10]
, NSBaselineOffsetAttributeName : @10} range:NSMakeRange(3, 1)];
How to make subscripts and superscripts using NSAttributedString?
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 NSAttributedString
s (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
.
Super/Subscript appear to be broken in iOS13 (NSAttributedString)
Simple 'fix' for this one.
It appears kCTSuperscriptAttributeName no longer works in iOS13 (for non-system fonts.) You need to use NSSuperscriptAttributeName instead. No idea where the definition for this lives (which header) so the actual string value required is "NSSuperScript"
How to display superscript % character as string in UIlabel?
I found this post on Stackoverflow on superscript styling text using attributed string:
NSAttributedString superscript styling
So using that, I hacked up this demo:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIFont *font = [UIFont fontWithName:@"Helvetica" size:20];
UILabel *textBlock1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 2.0)];
textBlock1.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
textBlock1.textAlignment = NSTextAlignmentCenter;
textBlock1.font = font;
textBlock1.text = @"57%";
UILabel *textBlock2 = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height / 2.0, self.view.bounds.size.width, self.view.bounds.size.height / 2.0)];
textBlock2.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
textBlock2.textAlignment = NSTextAlignmentCenter;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"57%"
attributes:@{NSFontAttributeName: font}];
[attributedString setAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:10]
, NSBaselineOffsetAttributeName : @10} range:NSMakeRange(2, 1)];
textBlock2.attributedText = attributedString;
[self.view addSubview:textBlock1];
[self.view addSubview:textBlock2];
}
The result:
Parsing HTML and then styling it with attributedString, how do I handle superscripts?
Don't use a UILabel, use Text
's AttributedString
constructor. See: How to use Attributed String in SwiftUI
How to change the allignment of a character in Attributed String
You need to use baselineOffset
of NSMutableAttributedString
calculate the diference between your two fonts sizes
let offset = baseFont.capHeight - smallFont.capHeight
Add the new attribute called baselineOffset
atrStr.addAttributes([NSAttributedString.Key.baselineOffset:offset], range: n1)
Is it possible to adjust the baseline in NSAttributedString?
As of iOS 7 the NSBaselineOffsetAttributeName
attribute has been added.
How do I use subscript and superscript in Swift?
Most of the answers+examples are in ObjC, but this is how to do it in Swift.
let font:UIFont? = UIFont(name: "Helvetica", size:20)
let fontSuper:UIFont? = UIFont(name: "Helvetica", size:10)
let attString:NSMutableAttributedString = NSMutableAttributedString(string: "6.022*1023", attributes: [.font:font!])
attString.setAttributes([.font:fontSuper!,.baselineOffset:10], range: NSRange(location:8,length:2))
labelVarName.attributedText = attString
This gives me:
In a more detailed explanation:
- Get
UIFont
you want for both the default and superscript style, superscript must be smaller. - Create a
NSMutableAttributedString
with the full string and default font. - Add an attribute to the characters you want to change (
NSRange
), with the smaller/subscriptUIFont
, and theNSBaselineOffsetAttributeName
value is the amount you want to offset it vertically. - Assign it to your
UILabel
Hopefully this helps other Swift devs as I needed this as well.
Related Topics
iOS Game Center: Scores Not Showing on Leaderboard in Sandbox
What Are the Options for Saving Data in iOS
How to Authenticate the Gklocalplayer on My 'Third Party Server'
How to Embed a Youtube Video into My App
What Describes the Application Delegate Best? How Does It Fit into the Whole Concept
Add Custom Controls to Avplayer in Swift
How to Detect Apps First Launch in iOS
Detecting If iPhone Is in a Dark Room
Checking for Multiple Asynchronous Responses from Alamofire and Swift
How to Keep a Round Imageview Round Using Auto Layout
How to Change the Background Color of the Uialertcontroller
Calculating Bearing Between Two Cllocation Points in Swift
How to Hide Status Bar in Uiimagepickercontroller
How to Disable Control Center in iOS 7 Programmatically and If Not, What Are Alternatives