1

I am trying to change the line spacing for a label to reduce the line space in Arabic language it is too much. The extension function I used from here with additions for Arabic styling is working on controlling the line spacing of the label but the only problem it leaves bottom margin white space I assume equals the same label size before reducing the line space.

The extension function here:

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

    paragraphStyle.alignment = .justified
    paragraphStyle.baseWritingDirection = .rightToLeft

    let attributedString:NSMutableAttributedString

    if let labelattributedText = self.attributedText {
        attributedString = NSMutableAttributedString(attributedString: labelattributedText)
    } else {
        attributedString = NSMutableAttributedString(string: labelText)
    }

    attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))

    self.attributedText = attributedString
}

}

then I just call the function like this:

bodyLabel.attributedText = entry.attributedText
bodyLabel.setLineSpacing(lineSpacing: -20)

enter image description here

daliaessam
  • 1,636
  • 2
  • 21
  • 43

2 Answers2

0

I tried the extension it's working fine like this:

bodyLabel.attributedText = entry.attributedText
bodyLabel.setLineSpacing(lineSpacing: -20)
bodyLabel.sizeToFit()

Also if that didn't work check the height for the label, and try setting the content to Fill.

bodyLabel.contentMode = .scaleAspectFill
Yasser
  • 265
  • 1
  • 8
0

Constraints of the bodyLabel must be like,

enter image description here

Adjust the bottom constraint of your label to >=0

PGDev
  • 23,751
  • 6
  • 34
  • 88