0

I'm trying to append attributed strings to make chord names like "G#m7" where the # and 7 are superscript. I'm doing it like this:

NSFont* font = [NSFont fontWithName:kGillSans size:24];
NSMutableDictionary* attributeNormal = [[NSMutableDictionary alloc] init];
[attributeNormal setObject:font forKey:NSFontAttributeName];
[attributeNormal setObject:color forKey:NSForegroundColorAttributeName];

NSFont* fontSmall = [NSFont fontWithName:kGillSans size:14];
NSMutableDictionary* attributeSuperScript = [[NSMutableDictionary alloc] init];
[attributeSuperScript setObject:fontSmall forKey:NSFontAttributeName];
[attributeSuperScript setObject:[NSNumber numberWithInteger:5] forKey:NSSuperscriptAttributeName];
[attributeSuperScript setObject:color forKey:NSForegroundColorAttributeName];


NSAttributedString* pitch = [[NSAttributedString alloc] initWithString:@"G" attributes:attributeNormal];

NSAttributedString* accidental = [[NSAttributedString alloc] initWithString:@"#" attributes:attributeSuperScript];

NSAttributedString* quality = [[NSAttributedString alloc] initWithString:@"m" attributes:attributeNormal];

NSAttributedString* seventh = [[NSAttributedString alloc] initWithString:@"7" attributes:attributeSuperScript];

NSMutableAttributedString* fullString = [[NSMutableAttributedString alloc] initWithAttributedString:pitch];
[fullString appendAttributedString:accidental];
[fullString appendAttributedString:quality];
[fullString appendAttributedString:seventh];

I then add this to a CATextLayer for display. Unfortunately, it's not working as expected. The superscripts are not superscript, and the [fullstring size] that is returned with it is way off, resulting in a hard to place layer. I turned the border width on to demonstrate the size issue.

enter image description here

I've experimented with different font sizes and values for the superscript attribute. Any ideas?

olynoise
  • 2,016
  • 2
  • 19
  • 32

1 Answers1

0

Try this:

NSFont* font = [NSFont fontWithName:@"GillSans" size:24];
NSDictionary * attributeNormal = @{NSFontAttributeName: font,
                                   NSForegroundColorAttributeName: [NSColor blackColor]};

NSFont* fontSmall = [NSFont fontWithName:@"GillSans" size:14];
NSDictionary * attributeSuperScript = @{NSFontAttributeName: fontSmall,
                                        NSSuperscriptAttributeName: @(2),
                                        NSForegroundColorAttributeName: [NSColor blackColor]};

NSMutableAttributedString* fullString =
[[NSMutableAttributedString alloc]
 initWithAttributedString:[[NSAttributedString alloc]
                           initWithString:@"G"
                           attributes:attributeNormal]];

[fullString appendAttributedString:
 [[NSAttributedString alloc] initWithString:@"#"
                                 attributes:attributeSuperScript]];

[fullString appendAttributedString:
 [[NSAttributedString alloc] initWithString:@"m"
                                 attributes:attributeNormal]];

[fullString appendAttributedString:
 [[NSAttributedString alloc] initWithString:@"7"
                                 attributes:attributeSuperScript]];
geowar
  • 4,397
  • 1
  • 28
  • 24
  • Just an FYI: I have a NSAttributedString category initWithArray: method that takes an array of attribute/strings to initialize a NSAttributedString: NSAttributedString* fullString = [[NSAttributedString alloc] initWithArray:@[attributeNormal, @"Welcome to ", attributeBold, @"GeoWorld", attributeNormal, @"! (Now go home)"]]; Message me if you'd like a copy. – geowar Jun 06 '13 at 16:40
  • I've put it on my dropbox at: . – geowar Jun 06 '13 at 16:46
  • Hey Thanks, but that's not really any different from my code, and the result is the same. – olynoise Jun 07 '13 at 19:45
  • The result looks find via [[myTextView textStorage] setAttributedString:fullString];. Perhaps the attributes are being stripped off/ignored by CATextLayer? What method are you using to set the string? – geowar Jun 10 '13 at 15:37