LayoutKit
LayoutKit copied to clipboard
TextViewLayout not showing all text
Playground code
import UIKit
import PlaygroundSupport
import LayoutKit
let rootView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 300))
rootView.backgroundColor = .white
let fontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .body)
let font = UIFont(descriptor: fontDescriptor, size: fontDescriptor.pointSize)
let attributedString = NSMutableAttributedString(
string: "Hello world! Testing TextViewLayout in LayoutKit.",
attributes: [
NSFontAttributeName: font,
]
)
let textViewLayout = TextViewLayout(
text: Text.attributed(attributedString)
)
let arrangement = textViewLayout.arrangement(width: rootView.bounds.width)
arrangement.makeViews(in: rootView)
Debug.addBorderColorsRecursively(rootView)
Debug.printRecursiveDescription(rootView)
PlaygroundPage.current.liveView = rootView
Results in the following view:
String "LayoutKit." is missing from TextView
Hi @spekke, thanks for your code and findings. I found LayoutKit
has the compatibility issue with UIFontDescriptor
. We are looking into it and in the meanwhile, could you use NSFontAttributeName: UIFont(name: font.fontName, size: font.pointSize)
instead?
Also, I found Xcode 9 beta 5 + iOS 11 doesn't have this issue. It only happens on Xcode 8.

@spekke I found the issue that by using UIFontDescriptor
, it provides different leading to compare with the default font, which is constructed by UIFont
initializer. After I added the font leading to the size calculation, the size is correct. I will create a PR to fix this. Thanks for reporting this!
Hi @spekke, thanks for your code and findings. I found LayoutKit has the compatibility issue with UIFontDescriptor. We are looking into it and in the meanwhile, could you use NSFontAttributeName: UIFont(name: font.fontName, size: font.pointSize) instead?
Also, I found Xcode 9 beta 5 + iOS 11 doesn't have this issue. It only happens on Xcode 8.
You're welcome. Thanks for looking into the problem. I have noticed the same thing - this problem seems to be fixed in iOS 11. The workaround you mentioned doesn't produce exactly the same result as using the font descriptor but good enough for now. Thanks!
@spekke I found the issue that by using UIFontDescriptor, it provides different leading to compare with the default font, which is constructed by UIFont initializer. After I added the font leading to the size calculation, the size is correct. I will create a PR to fix this. Thanks for reporting this!
Awesome, looking forward to the PR! :)
@jingwei-huang1 Any updates regarding this issue? Thanks
@spekke Thanks for your following up.
We are using boundingRect
to measure the size. However, I found boundingRect
gives the result without respecting UIFont
's leading
, which represents additional space between lines of text and is measured in points. In iOS 11, Apple fixed that issue.
I haven't got the chance to test all scenarios, which have the combinations of different attributes (ascender
, descender
, lineHeight
, etc) in TextView
. I will try to aim to complete it next week and will keep you posted.
Thanks!
@jingwei-huang1 Any updates on this?