Set UILabel line spacing
How can I modify the gap between lines in a multiline
- Is there a way to add custom fonts to Xcode to be available from a storyboard?
- Using custom fonts with Xcode 6 / iOS 8 Interface Builder Launch Screen
- UILabel min font size shadow issue
- Adding text over an image on Xcode
- Drop cap with NSAttributedString
- Objective C: How can you rotate text for UIButton and UILabel?
10 Solutions Collect From Internet About “Set UILabel line spacing”
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
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!
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 ;
You can control line spacing in the storyboard:
My solution was to patch the font file itself and fix its line height definitely.
I had to modify ‘lineGap’, ‘ascender’, ‘descender’ in the ‘hhea’ block (as in the blog example).
Best thing I found is: https://github.com/mattt/TTTAttributedLabel
It’s a UILabel subclass so you can just drop it in, and then to change the line height:
myLabel.lineHeightMultiple = 0.85; myLabel.leading = 2;
let label = UILabel() let stringValue = "Sample text" 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
Here’s some swift-code for you to set the line spacing programmatically
let label = UILabel() let attributedText = NSMutableAttributedString(string: "Your string") let paragraphStyle = NSMutableParagraphStyle() //SET THIS: paragraphStyle.lineSpacing = 4 //OR SET THIS: paragraphStyle.lineHeightMultiple = 4 //Or set both :) let range = NSMakeRange(0, attributedText.length) attributedText.addAttributes([NSParagraphStyleAttributeName : paragraphStyle], range: range) label.attributedText = attributedText
Of course, Mike’s answer doesn’t work if you pass the string programmatically. In this case you need to pass a attributed string and change it’s style.
NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:@"Your \nregular \nstring"]; NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; [style setLineSpacing:4]; [attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, attrString.length)]; _label.attributedText = attrString;
- kCLAuthorizationStatusNotDetermined occurred when tested with iOS 6.1 simulator
- Apple push notification is not working for distribution but working for development
- Difference between release and dealloc in objective-c
- UICollectionView horizontal scrolling, deleting last item, animation not working
- How to navigate through textfields (Next / Done Buttons)
- How to detect text (string) language in iOS?
- Creating an Objective-C equivalent Getter and Setter in Swift
- Request desktop sites using WKWebView in iOS
- Wrong method implementation address from otool for armv7?
- Check if NSURL is Local File
- Core Telephony framework partially public in 4.0
- Do methods called from within a block need to use weakSelf?
- Conditionally including a library for different iOS SDK versions?
- Cordova register file types to 'open with' list
- Uploading image to server in iphone application