objective-cswiftxcodeuilabelline-spacing

How to control the line spacing in UILabel


Is it possible to reduce the gap between text, when put in multiple lines in a UILabel? We can set the frame, font size and number of lines. I want to reduce the gap between the two lines in that label.


Solution

  • I thought about adding something new to this answer, so I don't feel as bad... Here is a Swift answer:

    import Cocoa
    
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = 40
    
    let attrString = NSMutableAttributedString(string: "Swift Answer")
    attrString.addAttribute(.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
    
    var tableViewCell = NSTableCellView()
    tableViewCell.textField.attributedStringValue = attrString
    

    "Short answer: you can't. To change the spacing between lines of text, you will have to subclass UILabel and roll your own drawTextInRect, or create multiple labels."

    See: Set UILabel line spacing


    This is a really old answer, and other have already addded the new and better way to handle this.. Please see the up to date answers provided below.