4

I'm using CustomCell instead of UITableViewCell on UITableView.

I put two UILables on CustomCell.

Refered this site: here , but I failed...

Question : How can I control line spacing of UILabels?

Krunal
  • 77,632
  • 48
  • 245
  • 261
hyekyung
  • 671
  • 2
  • 14
  • 27
  • possible duplicate of [How to increase a space between two lines in multiline label?](http://stackoverflow.com/questions/3880526/how-to-increase-a-space-between-two-lines-in-multiline-label) – rob mayoff Nov 24 '11 at 06:58
  • If you're having trouble with MTLabel, you can edit your question to describe what you did and what happened that you didn't like. – rob mayoff Nov 24 '11 at 06:59
  • i also having troubles with MTLabel and specially with IOS6. Can't it be done with Core text rather than third party classes? – iosMentalist Apr 08 '13 at 08:10

2 Answers2

13

Starting from iOS 6 you can set an attributed string to the UILabel. Check the following :

NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
[paragrahStyle setLineSpacing:40];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [labelText length])];

cell.label.attributedText = attributedString;
Rahul Mane
  • 1,005
  • 18
  • 33
iosMentalist
  • 3,066
  • 1
  • 30
  • 40
0

Programmatically:

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 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 = "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

// 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
Just copy & execute this code to see result.

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

Krunal
  • 77,632
  • 48
  • 245
  • 261