UILabel设置下划线:
UILabel
*label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 310, 50)];label.backgroundColor
= [UIColor redColor];[label
setLineBreakMode:NSLineBreakByWordWrapping];label.numberOfLines
=3;[label
setFont:[UIFont systemFontOfSize:14]];NSMutableAttributedString *slfstring
= [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"其他精彩博文http://blog.csdn.net/s651836663"]];NSRange slfstringRange
= {0,[slfstring length]};[slfstring
addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:slfstringRange];label.attributedText
=
slfstring;[self.view
addSubview:label];UIButton设置下划线:
UIButton
*button=[[UIButton alloc]initWithFrame:CGRectMake(10, 200, 80, 30)]; NSMutableAttributedString *title
= [[NSMutableAttributedString alloc]
initWithString:@"其他精彩博文http://blog.csdn.net/s651836663"]; NSRange titleRange
= {0,[title length]}; [title
addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange]; [button
setAttributedTitle:title forState:UIControlStateNormal]; [button
setBackgroundColor:[UIColor redColor]]; [button.titleLabel
setFont:[UIFont systemFontOfSize:14]]; [self.view
addSubview:button];
如果有些童鞋是通过XIB来实现label和button的也不用着急,将上述代码中的label和button的创建删除,换成你在XIB中创建的对象名即可改变下划线颜色:
[title addAttribute:NSUnderlineColorAttributeName value:[UIColor grayColor] range:(NSRange){0,[title length]}];
下划线样式:
NSUnderlineStyleNone = 0x00, 无下划线
NSUnderlineStyleSingle = 0x01, 单行下划线
NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0) = 0x02, 粗的下划线
NSUnderlineStyleDouble NS_ENUM_AVAILABLE(10_0, 7_0) = 0x09, 双下划线
这篇博客介绍了如何在XIB文件中为UIButton和UILabel设置下划线,并提供了设置不同样式的下划线方法,包括颜色和样式的选择,如单行、粗体和双下划线。
实现UIButton、UILabel文字添加下划线小技巧&spm=1001.2101.3001.5002&articleId=56668046&d=1&t=3&u=663c776382e84942888adc28f39ab51b)
1280

被折叠的 条评论
为什么被折叠?



