有没有办法修改下划线和NSAttributedString的线宽?
我似乎可以轻松修改颜色,但我无法轻易修改下划线本身的宽度。
答案 0 :(得分:3)
您可以设置NSUnderlineStyleThick
或NSUnderlineStyleSingle
,例如:
NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Thick underline" attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick)}];
NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Normal underline" attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}];
答案 1 :(得分:1)
//假设Label name为“label”
//下划线代码
CGSize expectedLabelSize = [@"Some text" sizeWithFont:label.font constrainedToSize:label.frame.size lineBreakMode:UILineBreakModeWordWrap];
UIView *viewForUnderline=[[UIView alloc] init];
viewForUnderline.frame=CGRectMake((label.frame.size.width - expectedLabelSize.width)/2, expectedLabelSize.height + (label.frame.size.height - expectedLabelSize.height)/2, expectedLabelSize.width, 1);
viewForUnderline.backgroundColor=[UIColor whiteColor];
[self.view addSubview:viewForUnderline];
或者您可以使用以下代码行
label.attributedText = [[NSAttributedString alloc] initWithString:@"Some Text"
attributes:underlineAttribute];