UILabel,下划线,不同的字体颜色

       今天项目用用到UILabel的attributedText来进行设置文本的不同显示,这个项目中使用应该会比较普遍,所以给大家分享下
       主要是用官方提供的attributedText的API,下面是代码

       UILabel下划线设置

       

        _changePhoneLabel = [[UILabel alloc]init];

        NSMutableAttributedString* content = [[NSMutableAttributedString alloc]initWithString:@"更换手机号"];
        NSRange contentRange = {0,[content length]};
        [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
        _changePhoneLabel.attributedText = content;
        _changePhoneLabel.textColor = C9;
        _changePhoneLabel.backgroundColor = CC;
        _changePhoneLabel.font = FONT14;

        _changePhoneLabel.textAlignment = NSTextAlignmentCenter;

       UILabel不同字体和颜色

      

            _oldPriceLabel = [[UILabel alloc]init];
            _oldPriceLabel.textAlignment = NSTextAlignmentLeft;
   
            NSString* oldPriceStr = [NSString stringWithFormat:@"售价¥4654.00"];
            NSMutableAttributedString* str = [[NSMutableAttributedString alloc]initWithString:oldPriceStr];
            [str addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x000000) range:NSMakeRange(0, 2)];
            [str addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xcc3333) range:NSMakeRange(2, 1)];
            [str addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xcc3333) range:NSMakeRange(3, str.length-5)];
            [str addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xcc3333) range:NSMakeRange(str.length-2, 2)];
            [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0] range:NSMakeRange(str.length-2, 2)];
            [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13.0] range:NSMakeRange(0, 2)];
            [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:11.0] range:NSMakeRange(2, 1)];
            [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0] range:NSMakeRange(3, str.length-5)];
            _oldPriceLabel.attributedText = str;

如图:

            

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值