Ravy Chheng
Ravy Chheng

Reputation: 173

Can't apply line spacing with UITextView?

Do you have any ideas about line spacing with UITextView? I cannot apply it. Below it is my code.

import UIKit

class DetailViewController: UIViewController {
    @IBOutlet weak var itemTextView: UITextView!
    var txtString: String?
    var txtTitleBar:String?

    override func viewDidLoad() {
        super.viewDidLoad()
        //navigationController?.navigationItem.title = "Love"
        self.title = txtTitleBar

        //self.tabBarController?.navigationItem.title = "My Title"
        itemTextView.text = txtString
        itemTextView.font = UIFont(name: "Arial-Regular", size:20)
        itemTextView.font = .systemFont(ofSize: 25)
        (itemTextView.font?.lineHeight)! * 5
    }
}

enter image description here

Upvotes: 13

Views: 19799

Answers (6)

kwiknik
kwiknik

Reputation: 885

Swift 5.7, iOS 16.0

Here's an extension to provide a textView.lineSpacing getter & setter:

extension UITextView {
    
    /// Gets & Sets line spacing via `typingAttributes`.
    /// Preserves defined `style` and `textColor`.
    var lineSpacing: CGFloat {
        get {
            if let style = typingAttributes[NSAttributedString.Key.paragraphStyle] {
                return (style as! NSMutableParagraphStyle).lineSpacing
            }
            return 0
        }
        set {
            let style = NSMutableParagraphStyle()
            style.lineSpacing = newValue
            let attributes = [
                NSAttributedString.Key.paragraphStyle: style,
                NSAttributedString.Key.foregroundColor: textColor,
                NSAttributedString.Key.font: font
            ]
            typingAttributes = attributes as [NSAttributedString.Key : Any]
        }
    }
}

It can be used when initialising the UITextView like so:

textView.font = UIFont.systemFont(ofSize: 18)
textView.textColor = UIColor.purple
textView.lineSpacing = 3.4

Upvotes: 0

garrettmurray
garrettmurray

Reputation: 3368

You need to use an attributed string and assign a paragraph style. For example:

let style = NSMutableParagraphStyle()
style.lineSpacing = 20
let attributes = [NSParagraphStyleAttributeName : style]
textView.attributedText = NSAttributedString(string: txtString, attributes: attributes)

See this SO answer for more details on attributed string usage: How do I make an attributed string using Swift?


Note, as of 2022 you do not need to use an attributed string. You can use .typingAttributes if preferred.

Upvotes: 29

Ahmad
Ahmad

Reputation: 353

Using typingAttributes will make sure that the attributes apply to new text that the user enters.

let style = NSMutableParagraphStyle()
style.lineSpacing = 10
let attributes = [NSAttributedString.Key.paragraphStyle : style]
textView.typingAttributes = attributes

Upvotes: 2

water_ak47
water_ak47

Reputation: 1306

let contentTextView: UITextView = {
    let textView = UITextView()

    textView.backgroundColor = .clear

    // Custom style
    let style = NSMutableParagraphStyle()
    style.lineSpacing = 10
    let attributes = [
        NSAttributedString.Key.paragraphStyle: style,
        NSAttributedString.Key.foregroundColor: UIColor.white,
        NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)
    ]
    textView.typingAttributes = attributes

    return textView
}()

This dictionary contains the attribute keys (and corresponding values) to apply to newly typed text. When the text view’s selection changes, the contents of the dictionary are cleared automatically.

https://developer.apple.com/documentation/uikit/uitextview/1618629-typingattributes

Upvotes: 0

Alex
Alex

Reputation: 71

Xcode 10.1 Swift 4.2

let style = NSMutableParagraphStyle()
style.lineSpacing = 19
let attributes = [NSAttributedString.Key.paragraphStyle: style]
textView.attributedText = NSAttributedString(string: model.text, attributes: attributes)

Upvotes: 4

Khawar Islam
Khawar Islam

Reputation: 2914

Xcode 9.2 Swift 4

let style = NSMutableParagraphStyle()
style.lineSpacing = 0
let attributes = [NSAttributedStringKey.paragraphStyle : style]
txtViewAbout.attributedText = NSAttributedString(string: txtViewAbout.text, attributes: attributes)

Upvotes: 5

Related Questions