在 UITextView 中控制行间距

Control Line Spacing in UITextView

我正在尝试删除 UITextView 中的行空格,但似乎无法弄清楚。 我尝试了 textContainer.lineFragmentPadding 和 NSLineBreakMode,但没有成功。

您应该像这样使用 NSAttributedString 而不是 String

let attributedString = NSMutableAttributedString(string: "Your text")

// *** Create instance of `NSMutableParagraphStyle`
let paragraphStyle = NSMutableParagraphStyle()

// *** set LineSpacing property in points ***
paragraphStyle.lineSpacing = 2 // Whatever line spacing you want in points

// *** Apply attribute to string ***
attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))
if let font = UIFont(name: "AvenirNextCondensed-Regular", size: font_size) {
    attributedString.addAttribute(NSAttributedString.Key.font, value: font, range: NSMakeRange(0, attributedString.length))
}

// *** Set Attributed String to your label ***
textView.attributedText = attributedString