1

I have an array of strings, I make a string from the array objects

let stringForLabel = arrayContaingStrings.joined(separator: "\n")

But when I show this it in a single UILabel, it is hard to read as below enter image description here

The 6th and 7th lines in the above label are a single line as

 Nowy Dwor Mazowiecki, Poland

But due to screen width is shown in 2 lines. So I would display it in the following format

enter image description here

How can I achieve this spacing in UIlabel? I have tried increasing line spacing in storyboard but then it increases all line spacing in the label. (The content in both images are different, I just wanted to show the line spacing in both cases)

Abin Baby
  • 586
  • 6
  • 17
  • If they are all in different labels, you can increase the spacing between the labels in layout? – Rakesha Shastri Jul 18 '18 at 10:46
  • It is a single UILabel – Abin Baby Jul 18 '18 at 10:47
  • Why do you need it to be in a single UILabel? – Rakesha Shastri Jul 18 '18 at 10:47
  • because there are many other contents like labels, textview, button etc are present down to this UIlabel and Now the height of UIlabel changes according to content. – Abin Baby Jul 18 '18 at 10:49
  • You can still attach those content below the last UILabel if you have every word in a label individually. If your problem is that it is a pain to create labels dynamically and set up constraints for them, then yes it is. – Rakesha Shastri Jul 18 '18 at 10:51
  • 2
    `let paragraphStyle = NSMutableParagraphStyle.init(); paragraphStyle.paragraphSpacing = 8.0; let attributedString = NSAttributedString.init(string: stringForLabel, attributes: [.paragraphStyle: paragraphStyle, .font: theFontYouWant]); label.attributedText = attributedString`? – Larme Jul 18 '18 at 10:52
  • @RakeshaShastri, Its dynamic content – Abin Baby Jul 18 '18 at 10:53
  • check this one https://stackoverflow.com/questions/43498344/objective-c-label-line-spacing – guru Jul 18 '18 at 10:55
  • @Larme Thank you, It works :) if you can post it as an answer I can accept it. – Abin Baby Jul 18 '18 at 11:01
  • Possible duplicate of [Objective C label line spacing?](https://stackoverflow.com/questions/43498344/objective-c-label-line-spacing) – Larme Jul 18 '18 at 11:51

1 Answers1

1

A paragraph is detected by the \n newline character. The line spacing between paragraphs and line spacing for lineBreakModes will be different.

You can use the paragraphSpacing to add extra spacing between paragraph compared to lineBreakMode spacing. Also try lineHeightMultiple.

Apple Docs link is provided above.

Try this UILabel 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
    paragraphStyle.paragraphSpacing = 10

    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
}
}
abhimuralidharan
  • 5,752
  • 5
  • 46
  • 70