Search code examples
nsattributedstringnsmutableattributedstring

How to use NSSuperscriptAttributeName for OS X


I can change the font and size but I'm stuck with making text a superscript.

Here is my working code for font and size:

aVerseMutableString = NSMutableAttributedString(string: book.verseText,
    attributes: [NSFontAttributeName:NSFont(name: "Helvetica", size: 18.0)!])

Here is what I'm trying for superscript that's not working:

aVerseNumberMutableString = NSMutableAttributedString(string: verseNumber.description, 
    attributes: [NSSuperscriptAttributeName:NSNumber(1)!])

I'm not sure how to do the attributes part to create a superscript.


Solution

  • After using NSBaselineOffsetAttributeName I figured out how to do it the way I showed above. Here are both ways:

    NSSuperscriptAttributeName

    aVerseNumberMutableString = NSMutableAttributedString(string: verseNumber.description, 
         attributes: [NSSuperscriptAttributeName:NSNumber(int: 2)])
    

    NSBaselineOffsetAttributeName

    aVerseNumberMutableString = NSMutableAttributedString(string: verseNumber.description, 
         attributes: [NSBaselineOffsetAttributeName:NSNumber(double: 2.0)])