Nsattributedstring Superscript Styling

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)];

Sample Image

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 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.

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:

Sample Image

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:

SuperScript Example

In a more detailed explanation:

  1. Get UIFont you want for both the default and superscript style, superscript must be smaller.
  2. Create a NSMutableAttributedString with the full string and default font.
  3. Add an attribute to the characters you want to change (NSRange), with the smaller/subscript UIFont, and the NSBaselineOffsetAttributeName value is the amount you want to offset it vertically.
  4. Assign it to your UILabel

Hopefully this helps other Swift devs as I needed this as well.



Related Topics



Leave a reply



Submit