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 number 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
let attrString = NSMutableAttributedString(string: "Swift Answer")
attrString.addAttribute(.paragraphStyle, 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."
这是一个非常古老的答案,其他人已经添加了新的更好的方法来处理这个问题.请参阅下面提供的最新答案.
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屋!