How can I underline text in a UITextView
. I understand that I would need to create a subclass of UITextView
, but what would go under drawRect
I recommend you to use CoreText. A Basic tutorial is here on raywenderlich.
If you want to avoid having to include CoreText, you can utilize an attributed string with this attribute:
@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}
textViewMessage.linkTextAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor], NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]};
-(IBAction)underline:(id)sender
{
NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
texts.attributedText = [[NSAttributedString alloc] initWithString:texts.text
attributes:underlineAttribute];
}
To underline a text, you have to go where you can select copy, cut , delete options there are now more options like B/I/U( bold, italic, underline). Choose this option and this is it. And to unable it, choose underline option again.
You can't use "kCTUnderlineStyleAttributeName" or "kCTUnderlineStyleSingle" Now you must do it like this:
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"Text"];
[attString addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:(NSRange){0,[attString length]}];