如何控制UILabel中的行间距 [英] How to control the line spacing in UILabel
问题描述
当在UILabel中放置多行时,是否可以减少文本之间的差距?我们可以设置框架,字体大小和#行。我想减少该标签中两行之间的差距。
Is it possible to reduce the gap between text when put in multiple lines in a UILabel? We can set the frame, font size and # of lines. I want to reduce the gap between the two lines in that label.
推荐答案
我想为此答案添加新内容,我不觉得那么坏...这是一个 Swift 答案:
I thought about adding something new to this answer, so I don't feel as bad... Here is a Swift answer:
import Cocoa
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 40
var attrString = NSMutableAttributedString(string: "Swift Answer")
attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
var tableViewCell = NSTableCellView()
tableViewCell.textField.attributedStringValue = attrString
简答:你不能。文本行,您将必须子类化UILabel并滚动自己的drawTextInRect,或创建多个标签。
"Short answer: you can't. To change the spacing between lines of text, you will have to subclass UILabel and roll your own drawTextInRect, or create multiple labels."
请参阅:设置UILabel行间距
这是一个真正的老答案,其他已经添加了新的更好的方式来处理这..请参见下面提供的最新答案。
This is a really old answer, and other have already addded the new and better way to handle this.. Please see the up to date answers provided below.
这篇关于如何控制UILabel中的行间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!