2

My target is to show cents as a superscript with small font in blue color. I am doing the following

        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;

However what i am getting is enter image description here and the superscript is not in blue.

Any thoughts about this one.

4

2 回答 2

4

这可能只是一个错误的属性名称问题,因为我怀疑您在此代码之前或之后没有明确地执行任何 CoreText 操作。

对于您的属性字符串,请尝试使用这些属性

[attString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(self.superScript.length - 2, self.superScript.length - 2)];
于 2013-07-09T16:22:19.320 回答
3

在 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 和 7,它也可以在 iOS 6 中使用。

于 2014-02-13T10:47:39.673 回答