ios 无法用UITextView应用行距?

mctunoxg  于 2023-02-26  发布在  iOS
关注(0)|答案(6)|浏览(233)

你对UITextView的行距有什么想法吗?我不能应用它。下面是我的代码。

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
    }
}

lp0sw83n

lp0sw83n1#

您需要使用属性化字符串并指定段落样式。例如:

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

有关属性化字符串用法的更多详细信息,请参见此SO答案:How do I make an attributed string using Swift?
注意,从2022年开始,你不需要使用属性化字符串。如果愿意,你可以使用.typingAttributes

shstlldc

shstlldc2#

Xcode 9.2快速移动4

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

c9qzyr3d3#

Xcode 10.1快速移动4.2

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

jei2mxaa4#

使用typingAttributes将确保属性应用于用户输入的新文本。

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

gupuwyp25#

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
}()

此字典包含应用于新键入文本的属性键(和相应值)。当文本视图的选择更改时,字典的内容将自动清除。
https://developer.apple.com/documentation/uikit/uitextview/1618629-typingattributes

z4bn682m

z4bn682m6#

  • 雨燕5.7,iOS 16.0*

下面是提供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]
        }
    }
}

它可以在初始化UITextView时使用,如下所示:

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

相关问题