Objective C - UILabel multiline vertical gap height

后端 未结 3 1371
遇见更好的自我
遇见更好的自我 2021-01-15 08:22

I have a multiline UILabel that can take a maximum of 3 lines. i.e message.numberOfLines = 3;

Everything works fine, but how can I set the vertical gap between the l

相关标签:
3条回答
  • 2021-01-15 08:49

    Unfortunately, the only good way to do this that I'm aware of is to draw the text yourself. On 3.2 or later, you can use CoreText to draw the text directly - I'm not sure if CT is available on iPhones >= 4.0. CoreText is a functional C API for flexible text drawing.

    0 讨论(0)
  • 2021-01-15 08:49

    I haven't been able to find a way to adjust the spacing between lines. The font property of UILabel has a number of read-only properties, so that won't help.

    I've resorted to draw my own text if I want to change the line spacing. I use NSString's -drawAtPoint and -drawInRect and use one or more of the -sizeWithFont methods to figure out how long the text will be in order to split the text and draw the right number of words or characters per line.

    0 讨论(0)
  • 2021-01-15 08:56

    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
    

    From Interface Builder:

    enter image description here

    0 讨论(0)
提交回复
热议问题