Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert sketch line height into ios line 'height multiple' property

enter image description here

My designer send me sketch file which says 'Line height: 22' for label. How can i achieve this in xcode interface builder. Is there any way to define this line height using code or UI builder.

like image 359
Mukesh Gami Avatar asked Jun 16 '17 08:06

Mukesh Gami


People also ask

How do I change the line height in SwiftUI?

SwiftUI text does not provide a lineHeight property (line spacing is a different beast). You could try to align the 'firstTextBaseLine' to get the desired behaviour. Alternatively, use a 'UILabel' (via 'UIViewRepresentable') with an attributed string (specify line height in the paragraph style).

How do you control line spacing in UILabel?

"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..


2 Answers

I've found the following formula to work well for me. It converts form Sketch line height to iOS line spacing:

lineSpacing = sketchLineHeight - sketchFontSize - (font.lineHeight - font.pointSize)

In code, for your case this would be:

let font = UIFont.systemFont(ofSize: 18) // or whatever font you use
textLabel.font = font
let attributedString = NSMutableAttributedString(string: "your text")
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 22 - 18 - (font.lineHeight - font.pointSize)
attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributedString.length))
textLabel.attributedText = attributedString
like image 181
bbjay Avatar answered Oct 25 '22 01:10

bbjay


@bbjay did put me on the right track.

If you want to obtain the exact result of Sketch, the formula is:

paragraphStyle .lineSpacing = sketchLineHeight - font.lineHeight

Provided that the font was given sketchFontSize

like image 25
Tancrede Chazallet Avatar answered Oct 24 '22 23:10

Tancrede Chazallet