iOS UIButton UILabel 等title 添加下划线

1,307 阅读1分钟

先看一下效果图: 效果图.png

直接上代码:

//忘记密码
NSMutableAttributedString *string1 = [[NSMutableAttributedString alloc]initWithString:@"忘记密码"];
    NSDictionary *attribute1 = @{NSForegroundColorAttributeName:RThemeColor,NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),NSFontAttributeName:[UIFont systemFontOfSize:15]};
    [string1addAttributes:attribute1range:NSMakeRange(0, string1.length)];
    self.forgetBtn.titleLabel.attributedText = string1;
//注册账号
    NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc]initWithString:@"注册账号"];
//attribute2 可以灵活改变
    NSDictionary *attribute2 = @{NSForegroundColorAttributeName:RThemeColor,NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),NSFontAttributeName:[UIFont systemFontOfSize:15]};
    [string2addAttributes:attribute1range:NSMakeRange(0, string2.length)];
    self.registBtn.titleLabel.attributedText = string2;