简体   繁体   中英

Control Line Spacing in UITextView

在此处输入图片说明

I'm trying to remove line spaces in an UITextView but can't seem to figure it out. I tried textContainer.lineFragmentPadding and NSLineBreakMode, but with no luck.

You should use NSAttributedString instead of String like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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