LayoutKit icon indicating copy to clipboard operation
LayoutKit copied to clipboard

TextViewLayout not showing all text

Open spekke opened this issue 7 years ago • 6 comments

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:

screen shot 2017-08-14 at 14 57 48

String "LayoutKit." is missing from TextView

spekke avatar Aug 14 '17 12:08 spekke

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.

font-descriptor-bug

jingwei-huang1 avatar Aug 14 '17 16:08 jingwei-huang1

@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!

jingwei-huang1 avatar Aug 14 '17 16:08 jingwei-huang1

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! :)

spekke avatar Aug 15 '17 11:08 spekke

@jingwei-huang1 Any updates regarding this issue? Thanks

spekke avatar Aug 31 '17 08:08 spekke

@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 avatar Sep 02 '17 05:09 jingwei-huang1

@jingwei-huang1 Any updates on this?

spekke avatar Dec 18 '17 16:12 spekke