Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust a UILabel line spacing programmatically in Swift?

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

like image 893
Pangu Avatar asked Jan 26 '16 19:01

Pangu


People also ask

How do I change line spacing in UILabel?

"Short answer: you can't. To change the spacing between lines of text, you will have to subclass UILabel and roll your own drawTextInRect, or create multiple labels." This is a really old answer, and other have already addded the new and better way to handle this..

How do I add a space between characters in Swift?

The two modifiers are tracking() and kerning() : both add spacing between letters, but tracking will pull apart ligatures whereas kerning will not, and kerning will leave some trailing whitespace whereas tracking will not.

What is line spacing in Swift?

Line spacing is the vertical distance between lines of text. Aim for about 140%-180% for optimal readability and accessibility. Limit line length to 70–80 characters. Font size should be minimum 16pt. The bigger the screen the bigger the text.

What is UILabel in Swift?

A view that displays one or more lines of informational text.


3 Answers

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

like image 197
Krunal Avatar answered Nov 09 '22 20:11

Krunal


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;
like image 29
DerailedLogic Avatar answered Nov 09 '22 20:11

DerailedLogic


Do this in the storyboard.....

enter image description here

like image 37
yogesh wadhwa Avatar answered Nov 09 '22 20:11

yogesh wadhwa