美文网首页iOS学习笔记iOS Developer
iOS自定义一个Label上显示不同大小不同颜色的字符串

iOS自定义一个Label上显示不同大小不同颜色的字符串

作者: Learning_ | 来源:发表于2016-06-12 18:37 被阅读3878次

UILabel *aLab = [[UILabel alloc]initWithFrame:CGRectMake(100,100,200,30)];

NSString *balance =@"888888";

NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"账户余额: %@元",balance]];

[aString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor]range:NSMakeRange(0,1)];

[aString addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor]range:NSMakeRange(1,1)];

[aString addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor]range:NSMakeRange(2,1)];

[aString addAttribute:NSForegroundColorAttributeName value:[UIColor purpleColor]range:NSMakeRange(3,1)];

[aString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor]range:NSMakeRange(6, balance.length)];

[aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15]range:NSMakeRange(6, balance.length)];

aLab.attributedText= aString;

[self.view addSubview:aLab];

效果如图:

😊

如果是简单的效果,就可以应用下面这个方法,当然也可以自己封装,或者参考大牛的demo

- (void)addAttribute:(NSString*)name value:(id)value range:(NSRange)range;

好记性不如烂笔头.

相关文章

网友评论

    本文标题:iOS自定义一个Label上显示不同大小不同颜色的字符串

    本文链接:https://www.haomeiwen.com/subject/uenedttx.html