ios 设置 UILabel 行距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3880526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set UILabel line spacing
提问by Matrix
How can I modify the gap between lines (line spacing) in a multiline UILabel?
如何修改多行中的行间距(行间距)UILabel?
回答by AndrewS
Edit:Evidently NSAttributedStringwill do it, on iOS 6 and later. Instead of using an NSStringto set the label's text, create an NSAttributedString, set attributes on it, then set it as the .attributedTexton the label. The code you want will be something like this:
编辑:显然NSAttributedString会在 iOS 6 及更高版本上做到这一点。不是使用 anNSString来设置标签的文本,而是创建一个 ,为其NSAttributedString设置属性,然后将其设置为.attributedText标签上的 。你想要的代码是这样的:
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 thatis being deprecated.
NSAttributedString的老attributedStringWithString做同样的事情,但现在这已被弃用。
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 UILabeland roll your own drawTextInRect, create multiple labels, or use a different font (perhaps one edited for a specific line height, see Phillipe's answer).
简短的回答:你不能。要更改文本行之间的间距,您必须将UILabel自己drawTextInRect的 .
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.
长答案:在印刷和网络世界中,文本行之间的空间被称为“领先”(与“标题”押韵,来自几十年前使用的铅金属)。前导是 的只读属性UIFont,在 4.0 中已弃用并由lineHeight. 据我所知,没有办法使用一组特定的参数来创建字体,例如lineHeight; 您可以获得系统字体和您添加的任何自定义字体,但一旦安装就无法调整它们。
There is no spacing parameter in UILabel, either.
中也没有间距参数UILabel。
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.
我对UILabel的行为并不特别满意,因此我建议编写您自己的子类或使用第 3 方库。这将使行为独立于您的字体选择,并成为最可重用的解决方案。
I wish there wasmore flexibility in UILabel, and I'd be happy to be proven wrong!
我希望有是在更多的灵活性UILabel,并且我很乐意被证明是错误的!
回答by iosMentalist
Starting in ios 6 you can set an attributed string in the UILabel:
从 ios 6 开始,您可以在 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 ;
回答by Mike S
回答by Krunal
From Interface Builder:
从接口生成器:


Programmatically:
以编程方式:
SWift 4
斯威夫特 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
斯威夫特 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
回答by Philippe
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/
我的解决方案是修补字体文件本身并确定其行高。 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).
我不得不修改“hhea”块中的“lineGap”、“ascender”、“descender”(如博客示例中所示)。
回答by adriendenat
This guy created a class to get line-height (without using CoreText, as MTLabel library) : https://github.com/LemonCake/MSLabel
这家伙创建了一个类来获取行高(不使用 CoreText,作为 MTLabel 库):https: //github.com/LemonCake/MSLabel
回答by lms
Best thing I found is: https://github.com/mattt/TTTAttributedLabel
我发现的最好的事情是:https: //github.com/mattt/TTTAttributedLabel
It's a UILabel subclass so you can just drop it in, and then to change the line height:
它是一个 UILabel 子类,因此您可以将其放入,然后更改行高:
myLabel.lineHeightMultiple = 0.85;
myLabel.leading = 2;
回答by Derek Bredensteiner
I've found 3rd Party Libraries Like this one:
我找到了像这样的第 3 方图书馆:
https://github.com/Tuszy/MTLabel
https://github.com/Tuszy/MTLabel
To be the easiest solution.
成为最简单的解决方案。
回答by ullstrm
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
回答by Ricardo Mutti
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.
当然,如果您以编程方式传递字符串,Mike 的答案将不起作用。在这种情况下,您需要传递一个属性字符串并更改它的样式。
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;

