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

The bottom of text is cut off when it's set to size returned from "sizeToFit" or "sizeThatFitsAttributedString" #658

Open
catjia1011 opened this issue Mar 28, 2016 · 11 comments

Comments

@catjia1011
Copy link

screen shot 2016-03-28 at 10 46 08 pm

I found this issue in my project with auto layout (content hugging). Then I did a test here.
The code is simple:

` UIFont *font = [UIFont systemFontOfSize:51];

NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:@"The quick fox jumps."];
[attrString addAttributes:@{
                            NSFontAttributeName: font,
                            NSForegroundColorAttributeName: [UIColor blackColor],
                            } range:NSMakeRange(0, attrString.length)];


self.label = [[UILabel alloc] init];
self.label.attributedText = attrString;
self.label.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
[self.label sizeToFit];
[self.view addSubview:self.label];

self.label2 = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
self.label2.attributedText = attrString;
self.label2.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
[self.label2 sizeToFit];
[self.view addSubview:self.label2];

self.label.center = CGPointMake(self.view.center.x, self.view.center.y - 50);
self.label2.center = CGPointMake(self.view.center.x, self.view.center.y + 50);

`

As shown in the picture, the bottom of the text is gone and the text is drawn with a strange offset (seems like descender from baseline). However, UILabel draws things well.

@jhersh
Copy link
Contributor

jhersh commented Mar 28, 2016

It is programmer error to assign to attributedText. From the readme:

TTTAttributedLabel can display both plain and attributed text: just pass an NSString or NSAttributedString to the setText: setter. Never assign to the attributedText property.

Can you retry by assigning to text? It's also unclear why you're setting a lineSpacing of -100.

@catjia1011
Copy link
Author

@jhersh sorry the "-100" was my testing code and just removed from my description. :p
I did test both the setText: and setAttributedText: methods and it was the same result.

@Coeur
Copy link

Coeur commented Mar 30, 2016

Latest version without the bug is TTTAttributedLabel 1.8.1
All versions from 1.9.1 to 1.13.4 have the issue.

@huynguyencong
Copy link

huynguyencong commented Apr 27, 2016

I used TTTAttributedLabel font size 30 and I had same problem. Thank @Coeur, I have used version 1.8.1 and have had no problem

@Coeur
Copy link

Coeur commented Apr 29, 2016

Issue was introduced on master with commit 7a08624 from 11 03 2014.
It is a "squash" of commits. I believe the one guilty sub-commit is 6e5fc1d

I'll now try to identify which line.

Coeur referenced this issue Apr 29, 2016
Remove all setNeedDisplay. If you want update, do this manual.
Fix the bug when mix Chinese and English lineheight error. Now draw line
offset y as -(font.descender+line descent). It's offset from label baseline.
Coeur referenced this issue Apr 29, 2016
commit 76a68999ffa236ed10093ca7a6da10e2e8815beb
Author: Mattt Thompson <m@mattt.me>
Date:   Tue Mar 11 04:19:24 2014 -0700

    Refactoring so that default behavior sets minimum and maximum line height to font line height, to accomodate mixed Western / Asian scripts

    Uncommenting calls to -setNeedsDisplay

commit 9f981f5
Author: qdvictory <qdvictory@gmail.com>
Date:   Fri Mar 7 12:04:57 2014 +0800

    set:
    self.attributedLabel.leading = -100;
    self.attributedLabel.maximumLineHeight = f.lineHeight;
    self.attributedLabel.minimumLineHeight = f.lineHeight;

commit fa1ca22
Author: qdvictory <qdvictory@gmail.com>
Date:   Fri Mar 7 12:04:14 2014 +0800

    add test Text

commit d1a3de8
Author: qdvictory <qdvictory@gmail.com>
Date:   Fri Mar 7 12:02:44 2014 +0800

    If you want fix linespace bug when mix Chinese & English, set leading = -100 and maximumLineHeight = minimumLineHeight = font.lineHeight.
    Make sure TTTLabel draw after Cell draw, and make sure setNeedsDisplay every time. Such as tableview reuse.

commit 6e5fc1d
Author: qdvictory <qdvictory@gmail.com>
Date:   Fri Mar 7 11:49:21 2014 +0800

    Add property minimumLineHeight and maximumLineHeight.
    Remove all setNeedDisplay. If you want update, do this manual.
    Fix the bug when mix Chinese and English lineheight error. Now draw line
    offset y as -(font.descender+line descent). It's offset from label baseline.

commit abf95e6
Merge: 4046ef4 4626f63
Author: Mattt Thompson <m@mattt.me>
Date:   Fri Feb 21 15:26:08 2014 -0800

    Merge pull request #324 from ScottPetit/master

    Updates TTTAttributedLabelVerticalAlignment to use NS_ENUM

commit 4626f63
Author: Scott Petit <petit.scott@gmail.com>
Date:   Fri Feb 21 06:58:04 2014 +0200

    - updates TTTAttributedLabelVerticalAlignment to use NS_ENUM, fixes warning when building to 64-Bit

Signed-off-by: Mattt Thompson <m@mattt.me>
@Coeur
Copy link

Coeur commented Apr 29, 2016

The string that was used to justify the commit was:

qdvictory: @门梁真可怕 当中英文Mix Chinese & English, Line Height has error.🐻🐻🐻🐻🐻🐻

It was later removed from tests with 7905eb3

@KhomenokV
Copy link

Same issue when using autoresizing label with autolayout (when height for label is not specified) and with HelveticaNeue font and fontSize 15 or 16 pt:
screen shot 2016-09-01 at 6 26 51 pm

Reproduces on the latest version and also on 1.8.1, 1.8.0, 1.7.0.

@Coeur
Copy link

Coeur commented Sep 2, 2016

@KhomenokV, did you try removing or modifying guilty lines like 6e5fc1d#commitcomment-17305527 ?

@KhomenokV
Copy link

KhomenokV commented Sep 2, 2016

I've checked it - unfortunately this doesn't fix the issue.

@Coeur
Copy link

Coeur commented Sep 5, 2016

As for you it affects versions older or equal to 1.8.1, it's probably a different issue.

@vzivanovvegait
Copy link

Set the same font size in storyboard (or NIB) and in NSFontAttributeName of NSMutableAttributedString, depends what you use in your project.

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

No branches or pull requests

6 participants