Reputation: 566
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:
?
Thanks.
Upvotes: 11
Views: 32128
Reputation: 1
let someString = NSMutableAttributedString(string: "Your String", attributes: [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.underlineStyle : NSUnderlineStyle.single.rawValue])
someStringTextView.attributedText = titleAT
U can just give your string a bunch of attributes like bold, underlined etc.
Upvotes: 0
Reputation: 281
Swift 5. As my UITextView if for inserting text, I created an extension function as bellow.
extension UITextView {
func underlined() {
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor.lightGray.cgColor
border.frame = CGRect(x: 0, y: self.frame.size.height - 5, width: self.frame.size.width, height: 1)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
let style = NSMutableParagraphStyle()
style.lineSpacing = 15
let attributes = [NSAttributedString.Key.paragraphStyle : style, NSAttributedString.Key.foregroundColor : UIColor.darkGray, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 13)]
self.attributedText = NSAttributedString(string: self.text, attributes: attributes)
}
}
The border is drawling the line and the style is adding the spacing between the lines. Usage in your UIView custom layout:
override func layoutSubviews() {
super.layoutSubviews()
self.dateAndTimeInput.underlined()
}
Upvotes: 0
Reputation: 4079
This is how I did it using Swift 5:
let attributedString = NSMutableAttributedString(string: myTextView.text ?? "")
myTextView.linkTextAttributes = [NSAttributedString.Key(rawValue: NSAttributedString.Key.underlineStyle.rawValue): NSUnderlineStyle.single.rawValue] as [NSAttributedString.Key: Any]?
myTextView.attributedText = attributedString
Upvotes: 0
Reputation: 341
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]}];
Upvotes: 1
Reputation: 69
-(IBAction)underline:(id)sender
{
NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
texts.attributedText = [[NSAttributedString alloc] initWithString:texts.text
attributes:underlineAttribute];
}
Upvotes: 1
Reputation: 8057
textViewMessage.linkTextAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor], NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]};
Upvotes: 2
Reputation: 36373
If this is static text, you can underline it in Interface Builder. Make sure to make the text 'Attributed' first by selecting 'Attributed' in the drop down menu:
Upvotes: 8
Reputation: 5655
If you want to avoid having to include CoreText, you can utilize an attributed string with this attribute:
@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}
Upvotes: 8
Reputation: 1
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.
Upvotes: -3
Reputation: 23278
Try to use NSAttributedString
as follows and set in UITextView
. This works for iOS6.
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"Some String"];
[attString addAttribute:(NSString*)kCTUnderlineStyleAttributeName
value:[NSNumber numberWithInt:kCTUnderlineStyleSingle]
range:(NSRange){0,[attString length]}];
For more info on NSAttributedString
check this How do you use NSAttributedString?
For eg:-
textView.attributedText = attString;
From apple documentation on UITextView,
In iOS 6 and later, this class supports multiple text styles through use of the attributedText property. (Styled text is not supported in earlier versions of iOS.) Setting a value for this property causes the text view to use the style information provided in the attributed string. You can still use the font, textColor, and textAlignment properties to set style attributes, but those properties apply to all of the text in the text view.
The styled text displayed by the text view.
@property(nonatomic,copy) NSAttributedString *attributedText
Discussion: This property is nil by default. Assigning a new value to this property also replaces the value of the text property with the same string data, albeit without any formatting information. In addition, assigning a new a value updates the values in the font, textColor, and textAlignment properties so that they reflect the style information starting at location 0 in the attributed string.
Upvotes: 23
Reputation: 4527
I recommend you to use CoreText. A Basic tutorial is here on raywenderlich.
Upvotes: 0
Reputation: 251
If you want to format your text (with underlined words, links, colored words...) I suggest you to use FTCoreText
Upvotes: 1
Reputation: 318934
If you are using iOS 6 then you can use the attributedText
attribute of UITextView. Apply underline formatting to the text. You can also set the typingAttributes
property to ensure the text that the user types has a specific set of formatting if you wish.
Upvotes: 0