Last Updated: December 26, 2018
·
23.73K
· 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 :

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