[ Modifying the width of an underline using attributed text? ]
Is there a a way to modify the line width of the underline an NSAttributedString?
It seems I can easily modify the color but I can't modify the width of the underline itself easily.
Answer 1
// assume Label name as "label"
// underline code
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];
or you can use following line of code
label.attributedText = [[NSAttributedString alloc] initWithString:@"Some Text"
attributes:underlineAttribute];
Answer 2
You can set NSUnderlineStyleThick
or NSUnderlineStyleSingle
, eg:
NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Thick underline" attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick)}];
NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Normal underline" attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}];
Full list of underline styles here: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/index.html#//apple_ref/c/tdef/NSUnderlineStyle