sr1ceq
Last Updated: July 10, 2018
·
14.2K
· novalagung
16162fed91387cc1cec055412db0022d

iOS - Change Line Height of UITextView

We can change line height of UITextView using NSMutableParagraphStyle by changing value of lineSpacing attribute.

NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = 17;
textView.attributedText = [[NSAttributedString alloc] 
    initWithString:@"Predefined Text" 
    attributes:@{NSParagraphStyleAttributeName : style}];
textView.text = @"Your text"

Example result :

Preview

if your UITextView's editable is enabled, you will see the caret is overflowing to top. you need to subclass UITextView and override caretRectForPosition: method to change height of the caret.

- (CGRect)caretRectForPosition:(UITextPosition *)position {
    CGRect originalRect = [super caretRectForPosition:position];
    originalRect.size.height = 18.;
    return originalRect;
}

useful link, see here : http://stackoverflow.com/questions/20207961/ios-uitextview-linespacing-make-cursor-height-not-same/20311650#20311650

Say Thanks
Respond

1 Response
Add your response

30144

thnkyou for this i was looking for this for a long time now
I'm already pretty sure that it can't be done with any public API, but I still want to ask:

Is there any way to change the line height in a UITextView?

Would be enough to do it statically, no need to change it at runtime. The problem is that the default line height is just WAY too small. Text will look extremely compressed and is a nightmare when trying to write longer texts.
best regards
https://gmaillogin.vip/ https://anydesk.vip/ https://downloader.vip/google-play-services/

about 1 month ago ·