Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 482
Ravy Chheng Avatar asked Mar 23 '17 02:03

Ravy Chheng


1 Answers

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?

like image 121
garrettmurray Avatar answered Oct 14 '22 16:10

garrettmurray