How to adjust a UILabel line spacing programmatically in Swift?
Asked Answered
E

4

7

I have a multiline UILabel as shown here:

enter image description here

I achieved this using the following code:

label.lineBreakMode = .ByWordWrapping
label.numberOfLines = 2

I'm trying to "decrease" the line spacing between the 1st line and 2nd line, and I tried to use the following code:

let text = label.attributedText
let mas = NSMutableAttributedString(attributedString:text!)
            mas.replaceCharactersInRange(NSMakeRange(0, mas.string.utf16.count),
                withString: label.text!)
label.attributedText = mas

However, it does not seem to work.

Thanks

Euchology answered 26/1, 2016 at 19:16 Comment(1)
I fixed the details.Euchology
N
19

Programmatically with Swift 4

Using label extension

extension UILabel {

    // Pass value for any one of both parameters and see result
    func setLineSpacing(lineSpacing: CGFloat = 0.0, lineHeightMultiple: CGFloat = 0.0) {

        guard let labelText = self.text else { return }

        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpacing
        paragraphStyle.lineHeightMultiple = lineHeightMultiple

        let attributedString:NSMutableAttributedString
        if let labelattributedText = self.attributedText {
            attributedString = NSMutableAttributedString(attributedString: labelattributedText)
        } else {
            attributedString = NSMutableAttributedString(string: labelText)
        }

        // Line spacing attribute
        attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))

        self.attributedText = attributedString
    }
}

Now call extension function

let label = UILabel()
let stringValue = "How\nto\nadjust\na\nUILabel\nline\nspacing\nprogrammatically\nin\nSwift"

// Pass value for any one argument - lineSpacing or lineHeightMultiple
label.setLineSpacing(lineSpacing: 2.0) .  // try values 1.0 to 5.0

// or try lineHeightMultiple
//label.setLineSpacing(lineHeightMultiple = 2.0) // try values 0.5 to 2.0

Or using label instance (Just copy & execute this code to see result)

let label = UILabel()
let stringValue = "How\nto\nadjust\na\nUILabel\nline\nspacing\nprogrammatically\nin\nSwift"
let attrString = NSMutableAttributedString(string: stringValue)
var style = NSMutableParagraphStyle()
style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40

// Line spacing attribute
attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: stringValue.characters.count))

// Character spacing attribute
attrString.addAttribute(NSAttributedStringKey.kern, value: 2, range: NSMakeRange(0, attrString.length))

label.attributedText = attrString

Swift 3

let label = UILabel()
let stringValue = "How to\ncontrol\nthe\nline spacing\nin UILabel"
let attrString = NSMutableAttributedString(string: stringValue)
var style = NSMutableParagraphStyle()
style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40
attrString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSRange(location: 0, length: stringValue.characters.count))
label.attributedText = attrString

From Interface Builder:

enter image description here

Nehru answered 18/3, 2017 at 8:47 Comment(2)
Using the Swift 4 solution I set lineHeightMultiple to 0.8, but it only removed the top of the line height. I still have padding at the bottom. If I decrease the multiple, it cuts off the top of the text, but continues to leave space at the bottom. Any ideas?En
@Nehru Any idea why the ellipsis text is gone if we use above code? Swift 5.2Matterhorn
L
14

You're on the right track with NSAttributedString. You need to set the line spacing of the paragraph style:

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = 30 // Whatever line spacing you want in points
    attributedString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))
    label.attributedText = attributedString;
Lay answered 26/1, 2016 at 19:32 Comment(2)
in this case, is attributedString = mas?...if so, it doesn't seem to work and the multi-line is now just one line with the last word cut offEuchology
So, multi-line isn't affected by NSAttributedString attributes. That value 30 is probably to big for you label and its making it impossible for it to go to 2 lines. Start at 0 and increase it til you get the spacing you want.Lay
M
2

Do this in the storyboard.....

enter image description here

Milli answered 9/8, 2017 at 13:10 Comment(0)
B
0
func updateLabel(with title: String) {
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineHeightMultiple = 0.8
    paragraphStyle.alignment = .center
    let string = NSAttributedString(string: title, attributes: [.paragraphStyle: paragraphStyle])
    label.attributedText = string
}
Butters answered 12/1, 2023 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.