Last Updated: December 26, 2018
· novalagung

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 :


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 :