0

我正在尝试附加属性字符串以制作像“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] 很远,导致很难放置层。我打开了边框宽度来演示尺寸问题。

在此处输入图像描述

我已经为上标属性尝试了不同的字体大小和值。有任何想法吗?

4

1 回答 1

0

尝试这个:

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]];
于 2013-06-06T16:08:02.807 回答