在开发的过程中,为了提醒用户我们经常会在label或者 button的文字下面添加下划线! cocoa给提供了一个NSString的子类
NSMutableAttributedString,他可以对字符串进行自定义编辑, 比如某个范围内字符串的字体的颜色,大小,当然还可以加下划线, 而UIButton 提供了 - (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state 接口
UIlabel 提供了@property(nullable, nonatomic,copy) NSAttributedString *attributedText;
固给Button添加下划线代码如下:
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"点击查看协议"];
NSRange strRange = {0,[str length]};
[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
[_awardDisplayBtn setAttributedTitle:str forState:UIControlStateNormal];