iOS 开发之给UILabel 或者 UIButton标题加下划线

来源:互联网 发布:苹果mac双系统切换 编辑:程序博客网 时间:2024/05/17 05:03

        在开发的过程中,为了提醒用户我们经常会在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]; 


0 0
原创粉丝点击