简体   繁体   English

在 UITextView 中控制行距

[英]Control Line Spacing in UITextView

在此处输入图片说明

I'm trying to remove line spaces in an UITextView but can't seem to figure it out.我正在尝试删除 UITextView 中的行空间,但似乎无法弄清楚。 I tried textContainer.lineFragmentPadding and NSLineBreakMode, but with no luck.我尝试了 textContainer.lineFragmentPadding 和 NSLineBreakMode,但没有运气。

You should use NSAttributedString instead of String like this:你应该像这样使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM