Set line height in UITextView

Viewed 54124

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.

thanks, Max

EDIT: I know that there is UIWebView and that it's nice and can do styling etc. But it's not editable. I need a editable text component with acceptable line height. That thing from the Omni Frameworks doesn't help either, as it's too slow and doesn't feel right...

9 Answers

This question is almost 10 years old but this is how it's done:

Just implement the following method of UITextViewDelegate and set your attributes:

let textViewAttributes: [NSAttributedString.Key:Any] = [
    .font: UIFont.systemFont(ofSize: 15, weight: .medium),
    .foregroundColor: UIColor.black,
    .paragraphStyle: {
        let paragraph = NSMutableParagraphStyle()
        paragraph.lineSpacing = 4
        return paragraph
    }()
]

func textViewDidBeginEditing(_ textView: UITextView) {
    textView.typingAttributes = textViewAttributes
}

It's important to add these attributes on textViewDidBeginEditing as the dictionary gets reset every time the text selection changes. More info can be found on the official documentation.

Related