我正在尝试附加属性字符串以制作像“G#m7”这样的和弦名称,其中 # 和 7 是上标。我这样做是这样的:
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];
然后我将它添加到 CATextLayer 中进行显示。不幸的是,它没有按预期工作。上标不是上标,和它一起返回的[fullstring size] 很远,导致很难放置层。我打开了边框宽度来演示尺寸问题。
我已经为上标属性尝试了不同的字体大小和值。有任何想法吗?