Superscripts in an attribute string

I am trying to get my shortcut to look like this:

enter image description here

But using the attributed string I was able to get this result:

enter image description here

My code:

NSString *string = [NSString stringWithFormat:@"%0.2f",ask];

NSMutableAttributedString *buyString = [[NSMutableAttributedString alloc] initWithString:string];

[buyString addAttribute:NSFontAttributeName
                  value:[UIFont systemFontOfSize:15.0]
                  range:NSMakeRange(2, buyString.length - 2)];

self.labelBuy.attributedText = buyString;

      

As you can see, the numbers after the dot remain below, and I would like to pull them out at the top as a first example. Is there a way to set an attributed inline frame?

+3


source to share


2 answers


You must use NSBaselineOffsetAttributedName

.

From the doc:

NSBaselineOffsetAttributeName


The value of this attribute is NSNumber

an object containing a floating point value that indicates the characters that are offset from the baseline, in points. The default is 0.
Available in iOS 7.0 and later.



In your example:

[buyString addAttribute:NSBaselineOffsetAttributeName
                  value:@(10.0)
                  range:NSMakeRange(2, buyString.length - 2)];

      

You may need to change the value to suit your needs.

+5


source


Why not superscript? You must first #import "CoreText / CoreText.h"



    [buyString addAttribute:(NSString *)kCTSuperscriptAttributeName 
                      value:@1 
                      range:NSMakeRange(2, buyString.length - 2)];

      

0


source







All Articles