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.
Asked
Active
Viewed 1,359 times
-1
-
Does this help? https://stackoverflow.com/questions/42966381/cant-apply-line-spacing-with-uitextview – Rob C Dec 11 '19 at 14:57
-
Yes. but then text.font = UIFont(name: "AvenirNextCondensed-Regular", size: font_size) stops working and the font size is off. And if I move the UIFont after the style, then the line space is off again. – Martin Vidic Dec 11 '19 at 15:55
1 Answers
3
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

shim
- 9,289
- 12
- 69
- 108

Arash Etemad
- 1,827
- 1
- 13
- 29
-
Thank you. I can control the line space like this. but as soon as I add style, the font size is off. text.font = UIFont(name: "AvenirNextCondensed-Regular", size: font_size) doesnt work anymore. – Martin Vidic Dec 11 '19 at 15:54