Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrectly displayed trailing text #26

Closed
srGritsuk opened this issue Dec 17, 2018 · 3 comments
Closed

Incorrectly displayed trailing text #26

srGritsuk opened this issue Dec 17, 2018 · 3 comments
Assignees
Labels
Milestone

Comments

@srGritsuk
Copy link
Contributor

srGritsuk commented Dec 17, 2018

What did you do

Set the text more than can be reflected

What did you expect to happen?

The test is displayed partially, and trailing text is displayed completely


Text text text text text text text text text text text text text te
xt text text text text text text text text text text text text text
text text text text text text text text text text text tex... More


What happened instead?

The test is displayed partially, and trailing text is displayed partially


Text text text text text text text text text text text text text te
xt text text text text text text text text text text text text text
text text text text text text text text text text text tex... Mor...


Why did it happen?

Text and trailing text have different attributes, so you need to use NSMutableAttributedString to calculate the height.

func willFit(to size: CGSize,
                 ellipsesString: String = "",
                 trailingText: String = "",
                 attributes: [NSAttributedString.Key: Any],
                 trailingTextAttributes: [NSAttributedString.Key: Any]) -> Bool {

        let attributedString = NSMutableAttributedString(string: self + ellipsesString, attributes: attributes)
        let attributedTrailingString = NSAttributedString(string: trailingText, attributes: trailingTextAttributes)
        attributedString.append(attributedTrailingString)
        
        let boundedSize = CGSize(width: size.width, height: .greatestFiniteMagnitude)
        let options: NSStringDrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading]
        let boundedRect = attributedString.boundingRect(with: boundedSize,
                                                        options: options,
                                                        context: nil)
        return boundedRect.height <= size.height
    }
@cgoldsby cgoldsby added the bug label Dec 17, 2018
@cgoldsby
Copy link
Owner

@srGritsuk, thank you for making the TvOSMoreButton better by submitting this excellent bug report. 🏅

I was able to reproduce this bug using the Example project and it does seem to be related to the trailing text attributes not being used when calculating the bounding rect.

screen shot 2018-12-17 at 8 00 54 am

Would you like to create a PR with the fix? I would love to have more contributors. If not; no worries.

@cgoldsby cgoldsby added this to the 1.2.2 milestone Dec 17, 2018
@srGritsuk
Copy link
Contributor Author

@cgoldsby, thanks for the quick response!
Of course, I would like to become a member of the project and create a PR with fix, and make the project even better in the future.

cgoldsby added a commit that referenced this issue Dec 18, 2018
…railing_text

#26 Implemented text height calculation using NSMutableAttributedString.
@cgoldsby
Copy link
Owner

@srGritsuk, thank you for your contribution, PR for fixing this bug, and, of course, interest in the TvOSMoreButton. 🍻

I published TvOSMoreButton (1.2.2) to CocoaPods. 🚀

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

No branches or pull requests

2 participants