Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set UILabel line spacing

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

like image 828
Matrix Avatar asked Oct 07 '10 09:10

Matrix


People also ask

Is line spacing the same as line height?

Line spacing is the amount of space between lines of text within a paragraph, the property set by “line-height” in HTML code. Line spacing is expressed in HTML as a number value or factor of the font size, such as 1.5× or 150%. As an example: 1.5× line height on size 12 text is 18 (by math 12 × 1.5).


2 Answers

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!

like image 94
AndrewS Avatar answered Sep 28 '22 20:09

AndrewS


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 ; 
like image 44
iosMentalist Avatar answered Sep 28 '22 21:09

iosMentalist