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, 双下划线