LinkedInAttic / LayoutKit

LayoutKit is a fast view layout library for iOS, macOS, and tvOS.
http://layoutkit.org
Apache License 2.0
3.16k stars 267 forks source link

Wrong LabelLayout height when using attributed string with kern and line height #184

Open plamenterziev opened 6 years ago

plamenterziev commented 6 years ago

For some reasons setting both kern and line height of an attributed string makes boundingRect to return wrong height if the string can fit only one line.

  let paragraphStyle = NSMutableParagraphStyle()
  paragraphStyle.lineSpacing = 100
  let text = NSMutableAttributedString(
       string: "test",
       attributes: [NSAttributedStringKey.paragraphStyle : paragraphStyle,
                           NSAttributedStringKey.kern : 0.1,
                           NSAttributedStringKey.font : UIFont.systemFont(ofSize: 17)])
  let size = text.boundingRect(with: CGSize(width: 1000, height: 1000),
                                                   options: [.usesLineFragmentOrigin], 
                                                   context: nil).size

Here the size is (29.59384765625, 120.287109375) which seems fine for width but the height is wrong (maybe is the real height + the line height).

If the text goes multilines then boundingRect returns correct height.

Also removing the kern attribute or setting it to 0 also fixes the problem.

itskoBits commented 6 years ago

I can reproduce this as well. Removing the kern spacing resolves the issue, but I want to use custom kerning.

itskoBits commented 6 years ago

This actually is a bug with NSAttriubutedString. You can see more https://stackoverflow.com/questions/48811307/wrong-height-for-uilabel-when-using-custom-linespacing-and-kern.