我的目标是以蓝色小字体显示美分作为上标。我正在做以下工作
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;然而,我得到的是

上标不是蓝色的。
对这个有什么想法吗。
发布于 2013-07-10 00:22:19
这可能只是一个错误的属性名称问题,因为我怀疑您在这段代码之前或之后没有显式地执行CoreText操作。
对于属性字符串,try using these attributes instead
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(self.superScript.length - 2, self.superScript.length - 2)];发布于 2014-02-13 18:47:40
在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中工作。
https://stackoverflow.com/questions/17553222
复制相似问题