iOS NSMutableAttributedString 实现富文本

项目中需要实现这么一条Label





 //金额标红

     //先将字符串以,隔开

     NSArray *totalMoenyArray = [totalMoneyText componentsSeparatedByString:@","];

        NSInteger moneyLastLength = 0;

     //如果隔开后有两个或以上字符串

     if (totalMoenyArray.count>1) {

          NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:totalMoneyText];

          for(int i=0;i<totalMoenyArray.count;i++){

              NSString *moneyStr= totalMoenyArray[i];

               NSInteger length = moneyStr.length;

               NSString *moneyLastStr = @"";

               if (i>0) {

                    moneyLastStr = totalMoenyArray[i-1];

                    moneyLastLength = moneyLastStr.length + moneyLastLength;

               }

          [str addAttribute:NSForegroundColorAttributeName value:RGB(226, 58, 58)range:NSMakeRange(3+moneyLastLength+i,length-4)];

          }

          self.totalMoneyLable.attributedText = str;

  

     }else{

          NSMutableAttributedString *countAttributeString = [[NSMutableAttributedString alloc]initWithString:totalMoneyText];

          [countAttributeString addAttributes:@{NSForegroundColorAttributeName:RGB(226, 58, 58)} range:NSMakeRange(3, totalMoneyText.length-4)];

          self.totalMoneyLable.attributedText = countAttributeString;

     }


//属性参数

NSFontAttributeName //字体 默认是12号

NSParagraphStyleAttributeName//段落样式

NSForegroundColorAttributeName//指定字体颜色

NSBackgroundColorAttributeName//字体背景颜色

NSLigatureAttributeName//连体字符

NSKernAttributeName//字距的像素

NSStrikethroughStyleAttributeName//指定字上加删除线

NSUnderlineStyleAttributeName//指定字加下划线

NSStrokeColorAttributeName//填充部分颜色

NSStrokeWidthAttributeName//填充宽度

NSShadowAttributeName//阴影

NSUnderlineColorAttributeName//下划线颜色



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值