设置下标、kCTForegroundColorAttributeName、ios的颜色

设置下标、kCTForegroundColorAttributeName、ios的颜色,ios,objective-c,nsstring,nsattributedstring,Ios,Objective C,Nsstring,Nsattributedstring,我的目标是用蓝色的小字体将美分显示为上标。我正在做以下工作 self.superScript = @"8899"; NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:self.superScript]; UIFont *font = [UIFont systemFontOfSize:18.0f];

我的目标是用蓝色的小字体将美分显示为上标。我正在做以下工作

        self.superScript      =   @"8899";
        NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:self.superScript];
        UIFont *font = [UIFont systemFontOfSize:18.0f];
        UIFont *smallFont = [UIFont systemFontOfSize:9.0f];

        [attString beginEditing];
        [attString addAttribute:NSFontAttributeName value:(font) range:NSMakeRange(0, self.superScript.length - 2)];
        [attString addAttribute:NSFontAttributeName value:(smallFont) range:NSMakeRange(self.superScript.length - 2, self.superScript.length - 2)];
        [attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"2" range:NSMakeRange(self.superScript.length - 2, self.superScript.length - 2)];
        [attString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)([[UIColor blueColor] CGColor]) range:NSMakeRange(self.superScript.length - 2, self.superScript.length - 2)];
        [attString endEditing];
        self.amount.attributedText = attString;
然而,我得到的是 上标不是蓝色的


对这个有什么想法吗

这可能只是一个错误的属性名称问题,因为我怀疑您在代码之前或之后没有做任何显式的CoreText操作

对于您的属性字符串:

在iOS7中

[attString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)([[UIColor blueColor] CGColor]) range:NSMakeRange(self.superScript.length - 2, self.superScript.length - 2)];
不起作用。将kCTForegroundColorAttributeName替换为

NSForegroundColorAttributeName
并传入一个常规UIColor对象以获取值

如果您需要支持iOS 6和iOS 7,它也可以在iOS 6中工作

NSForegroundColorAttributeName