美文网首页OC开发资料收集区
OC_字符串显示不同颜色

OC_字符串显示不同颜色

作者: 蜗牛锅 | 来源:发表于2017-05-17 15:02 被阅读48次
20170407235600405.png
 UILabel *lab = [[UILabel alloc]init];
 lab.frame = CGRectMake(20, 100, 300, 30);
 [self.view addSubview:lab];

   NSMutableAttributedString *aString;
    NSString *integralStr = @"integral";
    NSString *archStr = @"material";
    NSString *freightStr = @"shipping_fee";

 aString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"合计 积分:%@ 消费券:%@ (运费:%@)",integralStr,archStr,freightStr]];

 [aString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor]  range:NSMakeRange(6, integralStr.length)];
[aString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange( integralStr.length + 11, archStr.length)];
lab.attributedText = aString;

//


20170407235718782.png

相关文章

网友评论

    本文标题:OC_字符串显示不同颜色

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