Code Bye

如何设置一个UIlable里面字体有两种颜色拼接起来的

 

如何设置 积分:1000  这个uilable里面字体颜色 是积分黑色 1000 红色(这个是接口传进来的)


20分
没有电脑还不知道方法了,label有这个属性!明天发,

20分
self.title = @"For iOS 6 & later";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
attrLabel.attributedText = str;

http://my.oschina.net/CarlHuang/blog/138363


20分
可以通过NSMutableAttributedString来实现,如楼上。但在我看来,也可以分离“积分:”label 与显示积了多少分的label. 意思就是你可以各自创建,接口返回的值,直接显示,不需要再拼接“积分:”字符,还需要考虑如何显示不同颜色的问题。

20分
你的积分两个字是固定的就分开用两个labe,但是后面的那个“已领3天,明日可获取xxxx”可以用AttributedString,除了传统的做法以外,我们还可以通过占位符替换来实现,如:
“已领{Day}天”,然后把{Day}替换为服务器的返回值,这种方式也比较灵活

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明如何设置一个UIlable里面字体有两种颜色拼接起来的