如何增加 Swift 4 中 UILabel 的行距

Ene*_*nes 2 xcode uilabel swift nsparagraphstyle line-spacing

我想增加 UILabel 中的行距,但我不知道如何增加。

我在 Stackoverflow 上找到了这个解决方案 [ /sf/answers/2741108891/],但我的 Xcode 总是显示以下内容:

Use of unresolved identifier 'NSParagraphStyleAttributeName'
Run Code Online (Sandbox Code Playgroud)

我认为答案是正确的,但它对我不起作用。任何人都可以帮助解决这个问题吗?

Dzh*_*hek 9

雨燕5

import UIKit

extension UILabel {
    
    func setLineHeight(lineHeight: CGFloat) {
        guard let text = self.text else { return }
        
        let attributeString = NSMutableAttributedString(string: text)
        let style = NSMutableParagraphStyle()
        
        style.lineSpacing = lineHeight
        attributeString.addAttribute(
            NSAttributedString.Key.paragraphStyle,
            value: style,
            range: NSMakeRange(0, attributeString.length))
        
        self.attributedText = attributeString
    }

}
Run Code Online (Sandbox Code Playgroud)