Can't apply line spacing with UITextView?

前端 未结 3 523
广开言路
广开言路 2021-02-08 19:06

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

import UIKit

class DetailViewController: UIViewController {
           


        
相关标签:
3条回答
  • 2021-02-08 19:49

    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)
    
    0 讨论(0)
  • 2021-02-08 19:51

    Xcode 9.2 Swift 4

    let style = NSMutableParagraphStyle()
    style.lineSpacing = 0
    let attributes = [NSAttributedStringKey.paragraphStyle : style]
    txtViewAbout.attributedText = NSAttributedString(string: txtViewAbout.text, attributes: attributes)
    
    0 讨论(0)
  • 2021-02-08 19:57

    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?

    0 讨论(0)
提交回复
热议问题