Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

TextViewLayout not showing all text #151

Open
spekke opened this issue Aug 14, 2017 · 6 comments
Open

TextViewLayout not showing all text #151

spekke opened this issue Aug 14, 2017 · 6 comments
Assignees

Comments

@spekke
Copy link

spekke commented Aug 14, 2017

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

@jingwei-huang1
Copy link
Contributor

jingwei-huang1 commented Aug 14, 2017

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
Copy link
Contributor

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

@spekke
Copy link
Author

spekke commented Aug 15, 2017

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
Copy link
Author

spekke commented Aug 31, 2017

@jingwei-huang1 Any updates regarding this issue? Thanks

@jingwei-huang1
Copy link
Contributor

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

@spekke
Copy link
Author

spekke commented Dec 18, 2017

@jingwei-huang1 Any updates on this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants