iOS 5中的NSAttributedString设置文本字体和背景

3
可能是重复问题:
如何在iOS中设置NSAttributedString的属性?

我有以下代码:

 comment = [[NSMutableAttributedString alloc] initWithString:self.highlightItem_.comment];
            [comment addAttribute:NSFontAttributeName value:[UIFont fontWithName:kProximaNovaBold size:15] range:nameRange];
            [comment addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:128/255.f green:203/255.f blue:255/255.f alpha:1.0] range:nameRange];

            [comment addAttribute:NSFontAttributeName value:[UIFont fontWithName:kProximaNova size:15] range:commentRange];
            [comment addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithWhite:153/255.f alpha:1.0] range:commentRange];

这是基于iOS 6的代码,如果我想在iOS 5中实现同样的功能,我需要使用第三方库吗?


看看我的这个答案,也许对你有用。https://dev59.com/q2vXa4cB1Zd3GeqPIFrj#13579318 - Paras Joshi
1
这段代码也适用于iOS 5.0,NSMutableAttributedString从iOS 3.2开始可用。 - Ishu
@Ishu,NSFontAttributeName 实际上不起作用。 - adit
@ParasJoshi 有没有设置CoreText字体的方法? - adit
2
@adit 嘿,那段代码是用于CoreText的,请看一下我在回答中提供的教程链接:http://soulwithmobiletechnology.blogspot.in/2011/06/coretext-tutorial-for-ios-part-1.html :) - Paras Joshi
显示剩余2条评论
1个回答

0

你可以在iOS 5中使用Core Text。Core Text比NSAttributedString更强大,但文档也更少,对工程师来说不太容易访问。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接