Set UILabel line spacing
How can I modify the gap between lines in a multiline
- What is the best way to store an array of objects with cocoa?
- Why an empty implementation of drawRect: will adversely affect performance during animation
- How to flip a UIView around the x-axis while simultaneously switching subviews
- Average progress of all the NSURLSessionTasks in a NSURLSession
- in iPhone App How to detect the screen resolution of the device
- How to make a transparent UIWebView
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;
- iOS9 popover always points to top-left corner of anchor
- Bad converting String to NSDate
- GooglePlaces API “Response That We Couldn't Understand” Error
- Swift closure with Alamofire
- Xcode: Referencing C++ project from Objective-C project
- NSMutableData disappearing
- Sharing data in between apps in IOS
- Check if optional protocol method is implemented in Swift?
- Whatsapp integration and openURL issue in iOS 10
- Understanding optional global variables in swift
- camera overlay change with bearing and elevation
- How to change the corner radius of UISegmentedControl?
- How to set cursor position for UITextView on user input?
- Cocoa: Command /usr/bin/ditto failed with exit code 1
- Adding a Preprocessor Definition via a Xcode Scheme