字符串某个字符修改颜色、给字符串添加

字符串某个字符修改颜色:

UILabel *titleLabel = [[UILabel alloc]init];
titleLabel.font = smallFont;
titleLabel.text = @"*新密码";
titleLabel.textAlignment = NSTextAlignmentRight;
titleLabel.frame = lxy(5, 20+50, labelSize.width, 20);
NSMutableAttributedString *hintString=[[NSMutableAttributedString alloc]initWithString:titleLabel.text];
            //获取要调整颜色的文字位置,调整颜色
NSRange range=[[hintString string]rangeOfString:@"*"];
[hintString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
titleLabel.attributedText = hintString;

效果如图:

 

给字符串添加下划线:

    UILabel *registLabel = [[UILabel alloc]init];
    registLabel.text = @"新用户注册";
    registLabel.userInteractionEnabled = YES;
    CGSize size = [registLabel.text boundingRectWithSize:CGSizeMake(250, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:registLabel.font forKey:NSFontAttributeName] context:nil].size;
    registLabel.frame = lxy((SCREEN_WIDTH-size.width)/2, SCREEN_HEIGHT-20-30, size.width, size.height);
    [self.view addSubview:registLabel];

    NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:registLabel.text attributes:attribtDic];
    registLabel.attributedText = attribtStr;

效果图如下:

 

对一串字符串部分文字 进行字体颜色改变并加上下划线

    UILabel *lab_com = [[UILabel alloc]initWithFrame:lxy(0, KScreenH-navHeight-ShowDiff-40, KScreenW, 20)];

    lab_com.text = lxyName(@"Company_PrivacyPolicy");

    lab_com.font = FONT(18);

    lab_com.textColor = UIColor.whiteColor;

    lab_com.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:lab_com];

    

    NSMutableAttributedString *hintString=[[NSMutableAttributedString alloc]initWithString:lab_com.text];

    NSRange range=[[hintString string]rangeOfString:lxyName(@"PrivacyPolicy")];

    [hintString addAttribute:NSForegroundColorAttributeName value:UIBGColor range:range];

    

    NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};

    [hintString addAttributes:attribtDic range:range];

    

    lab_com.attributedText = hintString;

    lab_com.userInteractionEnabled = YES;

    

    UITapGestureRecognizer *privacyTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(privacyTapAction)];

    [lab_com addGestureRecognizer:privacyTap];

效果图如下

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值