我的目标是将美分显示为带蓝色小字体的上标。我正在做以下
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;
然而,我得到的是
和上标不是蓝色。
关于这个的任何想法。
答案 0 :(得分:4)
这可能只是一个错误的属性名称问题,因为我怀疑你在此代码之前或之后没有做任何明确的CoreText。
对于您的归因字符串,try using these attributes instead:
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(self.superScript.length - 2, self.superScript.length - 2)];
答案 1 :(得分:3)
在iOS7中
[attString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)([[UIColor blueColor] CGColor]) range:NSMakeRange(self.superScript.length - 2, self.superScript.length - 2)];
不起作用。用
替换kCTForegroundColorAttributeNameNSForegroundColorAttributeName
并传入一个常规的UIColor对象作为值。
如果你需要支持iOS 6& 7.