11

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

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Ravy Chheng
  • 153
  • 1
  • 2
  • 7

6 Answers6

27

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.

Fattie
  • 27,874
  • 70
  • 431
  • 719
garrettmurray
  • 3,338
  • 1
  • 25
  • 23
5

Xcode 9.2 Swift 4

let style = NSMutableParagraphStyle()
style.lineSpacing = 0
let attributes = [NSAttributedStringKey.paragraphStyle : style]
txtViewAbout.attributedText = NSAttributedString(string: txtViewAbout.text, attributes: attributes)
Khawar Islam
  • 2,556
  • 2
  • 34
  • 56
4

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)
LionHere
  • 9
  • 1
  • 2
Alex
  • 61
  • 7
2

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
McKinley
  • 1,123
  • 1
  • 8
  • 18
Ahmad
  • 348
  • 3
  • 14
0
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

water_ak47
  • 1,174
  • 12
  • 9
0

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
kwiknik
  • 570
  • 3
  • 7
  • doesn't work at all. the ONLY way now, 2023, is this https://stackoverflow.com/a/75997965/294884 – Fattie Apr 12 '23 at 16:48