Set UILabel line spacing

103,754

Solution 1

Edit: Evidently NSAttributedString will do it, on iOS 6 and later. Instead of using an NSString to set the label's text, create an NSAttributedString, set attributes on it, then set it as the .attributedText on the label. The code you want will be something like this:

NSMutableAttributedString* attrString = [[NSMutableAttributedString  alloc] initWithString:@"Sample text"];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:24];
[attrString addAttribute:NSParagraphStyleAttributeName
    value:style
    range:NSMakeRange(0, strLength)];
uiLabel.attributedText = attrString;

NSAttributedString's old attributedStringWithString did the same thing, but now that is being deprecated.

For historical reasons, here's my original answer:

Short answer: you can't. To change the spacing between lines of text, you will have to subclass UILabel and roll your own drawTextInRect, create multiple labels, or use a different font (perhaps one edited for a specific line height, see Phillipe's answer).

Long answer: In the print and online world, the space between lines of text is known as "leading" (rhymes with 'heading', and comes from the lead metal used decades ago). Leading is a read-only property of UIFont, which was deprecated in 4.0 and replaced by lineHeight. As far as I know, there's no way to create a font with a specific set of parameters such as lineHeight; you get the system fonts and any custom font you add, but can't tweak them once installed.

There is no spacing parameter in UILabel, either.

I'm not particularly happy with UILabel's behavior as is, so I suggest writing your own subclass or using a 3rd-party library. That will make the behavior independent of your font choice and be the most reusable solution.

I wish there was more flexibility in UILabel, and I'd be happy to be proven wrong!

Solution 2

Starting in ios 6 you can set an attributed string in the UILabel:

NSString *labelText = @"some text"; 
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:40];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
cell.label.attributedText = attributedString ;

Solution 3

You can control line spacing in the storyboard:

duplicate question

Solution 4

From Interface Builder:

enter image description here

Programmatically:

SWift 4

Using label extension

extension UILabel {

    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 to\ncontrol\nthe\nline spacing\nin UILabel"

// 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 = "Set\nUILabel\nline\nspacing"
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 = "Set\nUILabel\nline\nspacing"
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

Solution 5

My solution was to patch the font file itself and fix its line height definitely. http://mbauman.net/geek/2009/03/15/minor-truetype-font-editing-on-a-mac/

I had to modify 'lineGap', 'ascender', 'descender' in the 'hhea' block (as in the blog example).

Share:
103,754

Related videos on Youtube

Matrix
Author by

Matrix

I am working as an iphone application developer.

Updated on October 27, 2020

Comments

  • Matrix
    Matrix over 3 years

    How can I modify the gap between lines (line spacing) in a multiline UILabel?

  • Matrix
    Matrix over 13 years
    I have done it with sub-classing UILabel, basically idea is to divide label text into tokens, and then identity length of each token, create a separate label for each token and append one after another. Thats it.
  • Jonny
    Jonny over 11 years
    Super cool! Those OS X font tools also worked for my OTF font (although it only specifies TTF...). My font had line-height 1000 (!), I changed it to 0 and voila. I had miles and miles of empty space below the text on each line.
  • Philippe
    Philippe over 11 years
    Lies! ;) You can patch a font file to change the line height - see my answer on this page.
  • Kirk Woll
    Kirk Woll over 11 years
    I can't believe this is the best solution (no offense!), but it is by far the simplest. I used the free (Windows) software Type Light (and under Font | Metrics | Advanced you can modify lineGap) to edit my fonts. It also allows you to "rename" the fonts, which I couldn't figure out how to do using the tool Philippe mentioned.
  • Robbie
    Robbie over 11 years
    This is fantastic @Philippe ! Thanks for the post!!
  • Spencer Williams
    Spencer Williams over 11 years
    I'm led to believe there's a way to do this with NSAttributedString. If you need labels with attributed strings in pre iOS 6, check out OHAttributedLabel.
  • d.ennis
    d.ennis over 10 years
    Do it with NSAttributesString if you use iOS >= 6. Example
  • phatmann
    phatmann over 10 years
    Also see my answer to a similar question for more specifics: stackoverflow.com/a/19553827/201828
  • cyborg86pl
    cyborg86pl over 10 years
    thanks! i'd add that using AttributedString disables for example label's text alignment, so you have to add it to the paragraph style.
  • mbauman
    mbauman about 10 years
    @iamjustaprogrammer It's online again.
  • izk
    izk over 9 years
    However in Xcode 6.1.1 selecting the label and changing the line value in the attributed panel will cause the panel to flicker and lock the application. I have only been able to exit the panel by force-quitting Xcode.
  • Tieme
    Tieme over 9 years
    Thumbs up for the animation
  • MiguelSlv
    MiguelSlv over 8 years
    In version 7.1 the panel flicks and it doesn't have any effect on run time. Had to do it in code.
  • Sarasranglt
    Sarasranglt over 8 years
    440 watt electricity ran through my panel just now ;)
  • R. Mohan
    R. Mohan over 6 years
    This is Simple and Working Fine.
  • viral
    viral about 5 years
    The link is dead, again.
  • Arpit B Parekh
    Arpit B Parekh almost 5 years
    Need to pass only one arguement lineSpacing or multipleLineheight , can t pass both arguenemt other wise no result will come, i think