问题
I have a multiline UILabel that can take a maximum of 3 lines. i.e message.numberOfLines = 3;
Everything works fine, but how can I set the vertical gap between the lines? e.g between line 1 and line 2 etc?
Please enlight,
Tee
回答1:
I haven't been able to find a way to adjust the spacing between lines. The font property of UILabel has a number of read-only properties, so that won't help.
I've resorted to draw my own text if I want to change the line spacing. I use NSString's -drawAtPoint and -drawInRect and use one or more of the -sizeWithFont methods to figure out how long the text will be in order to split the text and draw the right number of words or characters per line.
回答2:
Unfortunately, the only good way to do this that I'm aware of is to draw the text yourself. On 3.2 or later, you can use CoreText to draw the text directly - I'm not sure if CT is available on iPhones >= 4.0. CoreText is a functional C API for flexible text drawing.
回答3:
Programmatically:
SWift 4
Using label extension
extension UILabel {
// Pass value for any one of both parameters and see result
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 = "How to\ncontrol\nthe\nline spacing\nin UILabel"
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
From Interface Builder:
来源:https://stackoverflow.com/questions/1737092/objective-c-uilabel-multiline-vertical-gap-height