Set UILabel line spacing

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

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

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

    Best thing I found is: https://github.com/mattt/TTTAttributedLabel

    It's a UILabel subclass so you can just drop it in, and then to change the line height:

    myLabel.lineHeightMultiple = 0.85;
    myLabel.leading = 2;
    
    0 讨论(0)
  • 2020-11-27 11:10

    Edit: Evidently NSAttributedString will do it, on iOS 6 and later. Instead of using an NSString to set the label's text, create an NSAttributedString, set attributes on it, then set it as the .attributedText on the label. The code you want will be something like this:

    NSMutableAttributedString* attrString = [[NSMutableAttributedString  alloc] initWithString:@"Sample text"];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:24];
    [attrString addAttribute:NSParagraphStyleAttributeName
        value:style
        range:NSMakeRange(0, strLength)];
    uiLabel.attributedText = attrString;
    

    NSAttributedString's old attributedStringWithString did the same thing, but now that is being deprecated.

    For historical reasons, here's my original answer:

    Short answer: you can't. To change the spacing between lines of text, you will have to subclass UILabel and roll your own drawTextInRect, create multiple labels, or use a different font (perhaps one edited for a specific line height, see Phillipe's answer).

    Long answer: In the print and online world, the space between lines of text is known as "leading" (rhymes with 'heading', and comes from the lead metal used decades ago). Leading is a read-only property of UIFont, which was deprecated in 4.0 and replaced by lineHeight. As far as I know, there's no way to create a font with a specific set of parameters such as lineHeight; you get the system fonts and any custom font you add, but can't tweak them once installed.

    There is no spacing parameter in UILabel, either.

    I'm not particularly happy with UILabel's behavior as is, so I suggest writing your own subclass or using a 3rd-party library. That will make the behavior independent of your font choice and be the most reusable solution.

    I wish there was more flexibility in UILabel, and I'd be happy to be proven wrong!

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

    Here's some swift-code for you to set the line spacing programmatically

    let label = UILabel()
    
    let attributedText = NSMutableAttributedString(string: "Your string")
    let paragraphStyle = NSMutableParagraphStyle()
    
    //SET THIS:
    paragraphStyle.lineSpacing = 4
    //OR SET THIS:
    paragraphStyle.lineHeightMultiple = 4
    
    //Or set both :)
    
    let range = NSMakeRange(0, attributedText.length)
    attributedText.addAttributes([NSParagraphStyleAttributeName : paragraphStyle], range: range)
    label.attributedText = attributedText
    
    0 讨论(0)
  • 2020-11-27 11:22

    Starting in ios 6 you can set an attributed string in the UILabel:

    NSString *labelText = @"some text"; 
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:40];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    cell.label.attributedText = attributedString ;
    
    0 讨论(0)
提交回复
热议问题