Xcode:有没有办法在界面构建器中更改行距(UI 标签)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12112657/
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
Xcode: Is there a way to change line spacing (UI Label) in interface builder?
提问by user1486548
I've got several UILabels with multiple lines of text, but the line spacing is larger than I would prefer. Is there any way to change this?
我有几个带有多行文本的 UILabels,但行距比我希望的要大。有什么办法可以改变这种情况吗?
采纳答案by Mazyod
Since iOS 6, Apple added NSAttributedString
to UIKit
, making it possible to use NSParagraphStyle
to change the line spacing.
从 iOS 6 开始,Apple 增加NSAttributedString
了UIKit
,使得可以NSParagraphStyle
用来改变行距。
To actually change it from NIB, please see souvickcse's answer.
要从 NIB 实际更改它,请参阅 souvickcse 的答案。
回答by souvickcse
hi this is a late reply but it may help some one line height can be change change the text from plain to attribute
嗨,这是一个迟到的回复,但它可能有助于某些单行高度可以更改将文本从纯文本更改为属性
回答by Logan Sease
Because I friggin hate using attributed text in interface builder (I always run into IB bugs), here is an extension to allow you to set line height multiple directly to a UILabel in interface builder
因为我讨厌在界面构建器中使用属性文本(我总是遇到 IB 错误),所以这里有一个扩展,允许您将行高倍数直接设置为界面构建器中的 UILabel
extension UILabel {
@IBInspectable
var lineHeightMultiple: CGFloat {
set{
//get our existing style or make a new one
let paragraphStyle: NSMutableParagraphStyle
if let existingStyle = attributedText?.attribute(NSAttributedString.Key.paragraphStyle, at: 0, effectiveRange: .none) as? NSParagraphStyle, let mutableCopy = existingStyle.mutableCopy() as? NSMutableParagraphStyle {
paragraphStyle = mutableCopy
} else {
paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 1.0
paragraphStyle.alignment = self.textAlignment
}
paragraphStyle.lineHeightMultiple = newValue
//set our text from existing text
let attrString = NSMutableAttributedString()
if let text = self.text {
attrString.append( NSMutableAttributedString(string: text))
attrString.addAttribute(NSAttributedString.Key.font, value: self.font, range: NSMakeRange(0, attrString.length))
}
else if let attributedText = self.attributedText {
attrString.append( attributedText)
}
//add our attributes and set the new text
attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
self.attributedText = attrString
}
get {
if let paragraphStyle = attributedText?.attribute(NSAttributedString.Key.paragraphStyle, at: 0, effectiveRange: .none) as? NSParagraphStyle {
return paragraphStyle.lineHeightMultiple
}
return 0
}
}