是否有可能减少文本之间的差距,当把在一个UILabel多行?我们可以设置框架,字体大小和行数。我想减少标签中两行之间的差距。


当前回答

我找到了一种方法,你可以设置真实的行高(不是一个因素),它甚至在界面生成器中渲染。按照下面的说明做就可以了。代码是用Swift 4编写的。


步骤#1:创建一个名为DesignableLabel.swift的文件,并插入以下代码:

import UIKit

@IBDesignable
class DesignableLabel: UILabel {
    @IBInspectable var lineHeight: CGFloat = 20 {
        didSet {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.minimumLineHeight = lineHeight
            paragraphStyle.maximumLineHeight = lineHeight
            paragraphStyle.alignment = self.textAlignment

            let attrString = NSMutableAttributedString(string: text!)
            attrString.addAttribute(NSAttributedStringKey.font, value: font, range: NSRange(location: 0, length: attrString.length))
            attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attrString.length))
            attributedText = attrString
        }
    }
}

步骤#2:将UILabel放入Storyboard/XIB中,并将其类设置为DesignableLabel。等待项目构建完成(构建必须成功!)。


步骤3:现在你应该在属性窗格中看到一个名为“Line Height”的新属性。只需设置您喜欢的值,您应该立即看到结果!

其他回答

根据@Mike的回答,减少lineHeightMultiple是关键点。下面的例子,它很适合我:

NSString* text = label.text;
CGFloat textWidth = [text sizeWithAttributes:@{NSFontAttributeName: label.font}].width;
if (textWidth > label.frame.size.width) {
  NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
  paragraph.alignment = NSTextAlignmentCenter;
  paragraph.lineSpacing = 1.0f;
  paragraph.lineHeightMultiple = 0.75;     // Reduce this value !!!
  NSMutableAttributedString* attrText = [[NSMutableAttributedString alloc] initWithString:text];
  [attrText addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, text.length)];
  label.attributedText = attrText;
}

这里提到的解决方案对我不起作用。我发现了一个稍微不同的方式来做它与iOS 6 NSAttributeString:

myLabel.numberOfLines = 0; 
NSString* string = @"String with line one. \n Line two. \n Line three.";
NSMutableParagraphStyle *style  = [[NSMutableParagraphStyle alloc] init];
style.minimumLineHeight = 30.f;
style.maximumLineHeight = 30.f;
NSDictionary *attributtes = @{NSParagraphStyleAttributeName : style,};
myLabel.attributedText = [[NSAttributedString alloc] initWithString:string
                                                         attributes:attributtes];   
[myLabel sizeToFit];

在Swift 2.0中…

添加一个扩展:

extension UIView {
    func attributesWithLineHeight(font: String, color: UIColor, fontSize: CGFloat, kern: Double, lineHeightMultiple: CGFloat) -> [String: NSObject] {
        let titleParagraphStyle = NSMutableParagraphStyle()
        titleParagraphStyle.lineHeightMultiple = lineHeightMultiple

        let attribute = [
            NSForegroundColorAttributeName: color,
            NSKernAttributeName: kern,
            NSFontAttributeName : UIFont(name: font, size: fontSize)!,
            NSParagraphStyleAttributeName: titleParagraphStyle
        ]
        return attribute
    }
}

现在,设置你的UILabel为attributedText:

self.label.attributedText = NSMutableAttributedString(string: "SwiftExample", attributes: attributesWithLineHeight("SourceSans-Regular", color: UIColor.whiteColor(), fontSize: 20, kern: 2.0, lineHeightMultiple: 0.5))    

显然,我添加了一堆您可能不需要的参数。玩周围-随时重写方法-我正在寻找这个在一堆不同的答案,所以我想我会张贴整个扩展,以防它帮助某人在那里…rab

在ios6中有另一个答案,那就是在标签上设置attributedText,使用NSAttributedString和适当的段落样式。关于NSAttributedString的行高,请参阅堆栈溢出答案:

NSAttributedString行高,对吗?

从ios6开始,你可以为UILabel设置带属性字符串。检查以下内容:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = spacing;
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, label.text.length)];

label.attributedText = attributedString;