Set UILabel line spacing

后端 未结 10 1464
鱼传尺愫
鱼传尺愫 2020-11-27 10:47

How can I modify the gap between lines (line spacing) in a multiline UILabel?

相关标签:
10条回答
  • 2020-11-27 11:00

    My solution was to patch the font file itself and fix its line height definitely. http://mbauman.net/geek/2009/03/15/minor-truetype-font-editing-on-a-mac/

    I had to modify 'lineGap', 'ascender', 'descender' in the 'hhea' block (as in the blog example).

    0 讨论(0)
  • 2020-11-27 11:00

    Of course, Mike's answer doesn't work if you pass the string programmatically. In this case you need to pass a attributed string and change it's style.

    NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:@"Your \nregular \nstring"];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:4];
    [attrString addAttribute:NSParagraphStyleAttributeName
                       value:style
                       range:NSMakeRange(0, attrString.length)];
    _label.attributedText = attrString;
    
    0 讨论(0)
  • 2020-11-27 11:01

    I've found 3rd Party Libraries Like this one:

    https://github.com/Tuszy/MTLabel

    To be the easiest solution.

    0 讨论(0)
  • 2020-11-27 11:03

    This guy created a class to get line-height (without using CoreText, as MTLabel library) : https://github.com/LemonCake/MSLabel

    0 讨论(0)
  • 2020-11-27 11:05

    You can control line spacing in the storyboard:

    duplicate question

    0 讨论(0)
  • 2020-11-27 11:05

    From Interface Builder:

    enter image description here

    Programmatically:

    SWift 4

    Using label 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
    
            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 = "Set\nUILabel\nline\nspacing"
    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
    

    Swift 3

    let label = UILabel()
    let stringValue = "Set\nUILabel\nline\nspacing"
    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
    attrString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSRange(location: 0, length: stringValue.characters.count))
    label.attributedText = attrString
    
    0 讨论(0)
提交回复
热议问题