xcode UILabel 行距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30386448/
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
UILabel Line Spacing
提问by Robert
I am trying to set the line spacing in a UILabel
as specified by Mike Slutsky here. It works correctly for the text I specify from the Storyboard. When I try to set the UILabel.text
in code, it reverts back to the default line spacing. Can someone help me understand how to either:
我正在尝试UILabel
按照 Mike Slutsky此处指定的方式设置行距。它适用于我从故事板指定的文本。当我尝试设置UILabel.text
in 代码时,它会恢复为默认行距。有人可以帮助我了解如何:
Keep it from reverting to default, and use the settings I specified on the Storyboard or
Set the value in code.
防止它恢复为默认值,并使用我在 Storyboard 上指定的设置或
在代码中设置值。
I've seen a lot of examples around using NSAttribute
and NSParagraph
, but since it's now possible to set in the Storyboard, I would expect their may be a more straightforward answer. Many thanks for the help!
我已经看到了很多关于使用NSAttribute
and的例子NSParagraph
,但由于现在可以在 Storyboard 中设置,我希望它们可能是一个更直接的答案。非常感谢您的帮助!
I set the "Height Multiple" as illustrated in the above link, and my only code is as follows:
我按照上面的链接设置了“高度倍数”,我唯一的代码如下:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textView: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textView.text = "Some example text"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
If I remove the textView.text line, it displays correctly, otherwise it's set back to the default line spacing (or Height Multiple).
如果我删除 textView.text 行,它会正确显示,否则它会设置回默认行距(或高度倍数)。
回答by matt
Here's what's going on. You set things up in the storyboard with custom line spacing. This means, even though you may not know it, that in the storyboard you have set the label's attributedText
.
这是怎么回事。您可以使用自定义行间距在故事板中进行设置。这意味着,即使您可能不知道,在情节提要中您已经设置了标签的attributedText
.
But if you then come along and set the label's text
in code, as you are doing, you throw away the attributedText
and therefore all the attributesthat it had. That is why, as you rightly say, things revert to the default look of the label.
但是,如果您随后text
在代码中设置标签,正如您所做的那样,您将丢弃它所具有的attributedText
所有属性。这就是为什么,正如您所说的,事情会恢复到标签的默认外观。
The solution is: instead of setting the label's text
, set its attributedText
. In particular, fetch the label's existing attributedText
; assign it into an NSMutableAttributedString so you can change it; replace its string while keeping the attributes; and assign it back to the label's attributedText
.
解决方案是:不要设置标签的text
,而是设置它的attributedText
. 特别是,获取标签的现有attributedText
; 将其分配给 NSMutableAttributedString 以便您可以更改它;在保留属性的同时替换其字符串;并将其分配回标签的attributedText
.
So for example (I have called my label lab
- your textView
is a bad choice, as a text view is whole different animal):
例如(我已经调用了我的标签lab
- 你textView
是一个糟糕的选择,因为文本视图是完全不同的动物):
let text = self.lab.attributedText
let mas = NSMutableAttributedString(attributedString:text)
mas.replaceCharactersInRange(NSMakeRange(0, count(mas.string.utf16)),
withString: "Little poltergeists make up the principle form of material manifestation")
self.lab.attributedText = mas
回答by Wingzero
UILabel has
UILabel 有
@property(nonatomic, copy) NSAttributedString *attributedText
since iOS 6.0,
@property(nonatomic, copy) NSAttributedString *attributedText
从 iOS 6.0 开始,
This property is nil by default. Assigning a new value to this property also replaces the value of the text property with the same string data, albeit without any formatting information. In addition, assigning a new a value updates the values in the font, textColor, and other style-related properties so that they reflect the style information starting at location 0 in the attributed string.
默认情况下,此属性为零。为该属性分配一个新值也会用相同的字符串数据替换 text 属性的值,尽管没有任何格式信息。此外,分配新的 a 值会更新 font、textColor 和其他与样式相关的属性中的值,以便它们反映从属性字符串中位置 0 开始的样式信息。
If you set textView.text = "Some example text"
again, you will loose your attributes. You should only pick one of them and not switching between them if you are sure what you are doing
如果你textView.text = "Some example text"
再次设置,你将失去你的属性。如果您确定自己在做什么,您应该只选择其中之一,不要在它们之间切换
回答by user2253546
Here is @matt 's answer in Obj. C:
这是@matt 在 Obj 中的回答。C:
NSMutableAttributedString *mas = [self.lab.attributedText mutableCopy];
[mas replaceCharactersInRange:NSMakeRange(0, [mas.string length])
withString:@"Little poltergeists make up the principle form of material manifestation"];
self.lab.attributedText = mas;
Hope this helps someone!
希望这对某人有帮助!
回答by Krunal
Swift 3Just copy & execute this code to see result
Swift 3只需复制并执行此代码即可查看结果
let label = UILabel()
let stringValue = "UILabel\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