Swift UILabel line spacing of break lines

586 views Asked by At

I have a text that comes from multiple sources and ends with \n\n for a line break in UILabel that has lines set to 0 through Storyboard.

For example:

let text = “Some text at the beginning of the paragraph that is this long\n\nSecond type of text\n\nSome longer text that is on later on in the paragraph”

The output is correct:

Some text at the beginning of the paragraph that is this long

Second type of text

Some longer text that is on later on in the paragraph

I have changed the line spacing to slightly increase the gaps between lines but can’t change the height of the empty line. I want the line break to be approximately half the size of a normal line break. I tried after/before paragraph settings but can’t get an empty line to be half the size.

Any idea if this is possible and what is the best way to achieve this through storyboard or programmatically.

----- Edit:

This is what I tried:

let paragraphStyle = NSMutableParagraphStyle()

            paragraphStyle.paragraphSpacing = 0.1
            paragraphStyle.paragraphSpacingBefore = 0.1
            
            let attrString = NSMutableAttributedString(string: text)
            attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
            
            cell.firstLabel?.attributedText = attrString
0

There are 0 answers