如何控制UILabel中的行距
在UILabel中放入多行时可以减less文本之间的差距吗? 我们可以设置框架,字体大小和行数。 我想缩小这个标签中两条线之间的差距。
我想为这个答案添加新的东西,所以我不觉得不好…这是一个迅速的答案:
import Cocoa let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = 40 let attrString = NSMutableAttributedString(string: "Swift Answer") attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString.length)) var tableViewCell = NSTableCellView() tableViewCell.textField.attributedStringValue = attrString
“简短的回答:你不能,要改变文本行之间的间距,你将不得不inheritanceUILabel,并展开你自己的drawTextInRect,或者创build多个标签。
请参阅: 设置UILabel行间距
这是一个非常古老的答案,而其他已经添加了新的更好的方式来处理这个..请参阅下面提供的最新答案。
在Xcode 6中,您可以在故事板中执行此操作:
从iOS 6开始,您可以将一个属性string设置为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;
这里所说的解决scheme对我来说不起作用。 我发现了一个与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];
现在在iOS 6中有一个替代答案,就是在标签上设置属性文字,使用NSAttributedString和相应的段落样式。 有关NSAttributedString的行高的详细信息,请参阅此堆栈溢出答案:
核心文本 – NSAttributedString行高正确?
我做了这个简单的扩展,对我来说效果很好:
extension UILabel { func setLineHeight(lineHeight: CGFloat) { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = 1.0 paragraphStyle.lineHeightMultiple = lineHeight paragraphStyle.alignment = self.textAlignment let attrString = NSMutableAttributedString(string: self.text!) attrString.addAttribute(NSFontAttributeName, value: self.font, range: NSMakeRange(0, attrString.length)) attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString.length)) self.attributedText = attrString } }
将其复制到一个文件中,然后可以像这样使用它
myLabel.setLineHeight(0.7)
这是一个UILabel子类的行高属性的类: https : //github.com/LemonCake/MSLabel
在Swift中作为一个函数,受到DarkDust的启发
// Usage: setTextWithLineSpacing(myEpicUILabel,text:"Hello",lineSpacing:20) func setTextWithLineSpacing(label:UILabel,text:String,lineSpacing:CGFloat) { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = lineSpacing let attrString = NSMutableAttributedString(string: text) attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString.length)) label.attributedText = attrString }
根据麦克的回答,减less行lineHeightMultiple
是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; }
SWIFT 3有用的扩展设置空间之间的线更容易:)
extension UILabel { func setLineHeight(lineHeight: CGFloat) { let text = self.text if let text = text { let attributeString = NSMutableAttributedString(string: text) let style = NSMutableParagraphStyle() style.lineSpacing = lineHeight attributeString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSMakeRange(0, text.characters.count)) self.attributedText = attributeString } } }
从界面生成器:
编程方式:
SWIFT 4
使用标签扩展
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 } }
现在调用扩展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
或者使用标签实例(只需复制并执行此代码即可查看结果)
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
Swift 3
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 attrString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSRange(location: 0, length: stringValue.characters.count)) label.attributedText = attrString
在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设置为属性文本:
self.label.attributedText = NSMutableAttributedString(string: "SwiftExample", attributes: attributesWithLineHeight("SourceSans-Regular", color: UIColor.whiteColor(), fontSize: 20, kern: 2.0, lineHeightMultiple: 0.5))
显然,我添加了一堆你可能不需要的参数。 玩耍 – 随意重写方法 – 我正在寻找一堆不同的答案,所以我想张贴整个扩展,以防止有人在那里… -rab
Swift3 – 在UITextView或UILabel扩展中,添加这个函数:
我添加了一些代码来保留当前的属性文本,如果你已经使用视图的属性string(而不是覆盖它们)。
func setLineHeight(_ lineHeight: CGFloat) { guard let text = self.text, let font = self.font else { return } let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = 1.0 paragraphStyle.lineHeightMultiple = lineHeight paragraphStyle.alignment = self.textAlignment var attrString:NSMutableAttributedString if let attributed = self.attributedText { attrString = NSMutableAttributedString(attributedString: attributed) } else { attrString = NSMutableAttributedString(string: text) attrString.addAttribute(NSFontAttributeName, value: font, range: NSMakeRange(0, attrString.length)) } attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString.length)) self.attributedText = attrString }
我find了一种方法可以设置真正的行高 (不是一个因素),甚至可以在Interface Builder中生效 。 只要按照下面的说明。 代码是用Swift 4编写的。
步骤#1:创build一个名为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 } } }
第二步:将一个UILabel
放入一个Storyboard / XIB,并将其类设置为DesignableLabel
。 等待你的项目build立(build立必须成功!)。
第3步:现在,您应该在名为“行高”的属性窗格中看到新的属性。 只需设置你喜欢的值,你应该马上看到结果!
这段代码适用于我(当然是ios 7&ios 8)。
_label.numberOfLines=2; _label.textColor=[UIColor whiteColor]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineHeightMultiple=0.5; paragraphStyle.alignment = NSTextAlignmentCenter; paragraphStyle.lineSpacing = 1.0; NSDictionary *nameAttributes=@{ NSParagraphStyleAttributeName : paragraphStyle, NSBaselineOffsetAttributeName:@2.0 }; NSAttributedString *string=[[NSAttributedString alloc] initWithString:@"22m\nago" attributes:nameAttributes]; _label.attributedText=string;
另一个答案…如果你以编程方式传递string,你需要传递一个属性string,而不是一个普通的string,并改变它的样式(iOS10)
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;
斯威夫特3延伸:
import UIKit extension UILabel { func setTextWithLineSpacing(text: String, lineHeightMultiply: CGFloat = 1.3) { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineHeightMultiple = lineHeightMultiply paragraphStyle.alignment = .center let attributedString = NSMutableAttributedString(string: text) attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length)) self.attributedText = attributedString } }
这应该有所帮助。 然后,您可以将标签分配到故事板中的此自定义类,并直接在属性中使用它的参数:
open class SpacingLabel : UILabel { @IBInspectable open var lineHeight:CGFloat = 1 { didSet { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = 1.0 paragraphStyle.lineHeightMultiple = self.lineHeight paragraphStyle.alignment = self.textAlignment let attrString = NSMutableAttributedString(string: self.text!) attrString.addAttribute(NSAttributedStringKey.font, value: self.font, range: NSMakeRange(0, attrString.length)) attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length)) self.attributedText = attrString } } }
作为一个快速肮脏聪明简单的解决方法:
对于没有太多行的UILabel,你可以使用stackViews。
- 为每一行写一个新的标签。
- 将它们embedded到StackView中(同时select标签 – >编辑 – >embedded – > StackView
- 调整StackView的
Spacing
到你想要的数量
一定要垂直堆放。 此解决scheme也适用于自定义字体。