其本质就是button设置setAttributedTitle
所以我们必行定义一个NSMutableAttributedString类型的可变字符串
具体步骤如下所示
NSMutableAttributedString* mString = [[NSMutableAttributedString alloc] initWithString:@"Not Snoring?"];
[mString addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:(NSRange){0,[mString length]}];
//设置字体颜色
[mString addAttribute:NSForegroundColorAttributeName value:kColorRGBA(0x338eff, 1) range:NSMakeRange(0,[mString length])];
//设置下划线颜色
[mString addAttribute:NSUnderlineColorAttributeName value:kColorRGBA(0x338eff, 1) range:(NSRange){0,[mString length]}];
[self.reportButton setAttributedTitle:mString forState:0];
这样button设置下划线即完成
同理label亦如此
最后附上属性表
NSFontAttributeName 字体
NSParagraphStyleAttributeName 段落格式
NSForegroundColorAttributeName 字体颜色
NSBackgroundColorAttributeName 背景颜色
NSStrikethroughStyleAttributeName 删除线格式
NSUnderlineStyleAttributeName 下划线格式
NSStrokeColorAttributeName 删除线颜色
NSStrokeWidthAttributeName 删除线宽度
NSShadowAttributeName
网友评论