首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置下标、kCTForegroundColorAttributeName、ios的颜色

设置下标、kCTForegroundColorAttributeName、ios的颜色
EN

Stack Overflow用户
提问于 2013-07-10 00:15:19
回答 2查看 1.9K关注 0票数 2

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

代码语言:javascript
复制
        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;

然而,我得到的是

上标不是蓝色的。

对这个有什么想法吗。

EN

回答 2

Stack Overflow用户

发布于 2013-07-10 00:22:19

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

对于属性字符串,try using these attributes instead

代码语言:javascript
复制
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(self.superScript.length - 2, self.superScript.length - 2)];
票数 4
EN

Stack Overflow用户

发布于 2014-02-13 18:47:40

在iOS7中

代码语言:javascript
复制
[attString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)([[UIColor blueColor] CGColor]) range:NSMakeRange(self.superScript.length - 2, self.superScript.length - 2)];

不起作用。将kCTForegroundColorAttributeName替换为

代码语言:javascript
复制
NSForegroundColorAttributeName

并传入一个常规的UIColor对象作为值。

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17553222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档