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

SizeToFit return always 21.0 for height #151

Closed
mladjan opened this issue Jul 14, 2013 · 2 comments
Closed

SizeToFit return always 21.0 for height #151

mladjan opened this issue Jul 14, 2013 · 2 comments

Comments

@mladjan
Copy link

mladjan commented Jul 14, 2013

Here is my code, and I don't know where I'm making a mistake.
I don't use frames, I need only height so I can set height autolayout constraint from code.

               OHAttributedLabel *tv = [[OHAttributedLabel alloc] init];

                [tv setTag:[[contentElement attributeNamed:@"id"] intValue]];
                [tv setTranslatesAutoresizingMaskIntoConstraints:NO];

                NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:[contentElement value]];
                OHParagraphStyle* paragraphStyle = [OHParagraphStyle defaultParagraphStyle];
                UIFont *font;
                if(self.fontName && [contentElement attributeNamed:@"size"]){
                    font = [UIFont fontWithName:self.fontName size:[[contentElement attributeNamed:@"size"] intValue]];
                }else{
                    font = [UIFont systemFontOfSize:16];
                }
                [attrStr setFont:font];
                if([[contentElement attributeNamed:@"align"] isEqualToString:@"left"]){
                    paragraphStyle.textAlignment = kCTLeftTextAlignment;
                }else if([[contentElement attributeNamed:@"align"] isEqualToString:@"right"]){
                    paragraphStyle.textAlignment = kCTRightTextAlignment;
                }else{
                    paragraphStyle.textAlignment = kCTJustifiedTextAlignment;
                }
                if([contentElement attributeNamed:@"color"]){
                    NSUInteger red, green, blue;
                    sscanf([[contentElement attributeNamed:@"color"] UTF8String], "#%02X%02X%02X", &red, &green, &blue);
                    UIColor *color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
                    [tv setTextColor:color];
                }else{
                    [tv setTextColor:[UIColor blackColor]];
                }

                paragraphStyle.lineBreakMode = kCTLineBreakByWordWrapping;
                paragraphStyle.firstLineHeadIndent = 30.f; // indentation for first line
                paragraphStyle.lineSpacing = 3.f; // increase space between lines by 3 points

                [attrStr setParagraphStyle:paragraphStyle];
                tv.numberOfLines = 0;
                tv.lineBreakMode = NSLineBreakByWordWrapping;

                tv.attributedText = [OHASBasicHTMLParser attributedStringByProcessingMarkupInAttributedString:attrStr];
                [tv setBackgroundColor:[UIColor clearColor]];
                [tv sizeToFit];

                [self.containerView addSubview:tv];
@AliSoftware
Copy link
Owner

Did you dig in with a breakpoint on sizeToFit and step into?

Did you try with methods dedicated to string size computations in my NSAttributedString category instead of sizeToFit?

@AliSoftware
Copy link
Owner

Actually after re-reading your code this is probably normal if your text only fit in one line (see maybe related #3) and your font has a lineHeight of 21, right?

I'm not sure using both sizeToFit and AutoLayout is compatible (this assertion is not related to OHAttributedLabel only, and is true for any UIView). AutoLayout is supposed to adapt the frame of your views by applying constraints. Calling sizeToFit also try to change this frame, but AutoLayout has always higher priority.

When you use AutoLayout, changing a view.frame with [UIView setFrame:] and such methods (which obviously sizeToFit does as stated in the Apple documentation) won't work (won't do anything), because AutoLayout will apply its constraints and change the frame back by itself afterwards.

According to my tests I'm guessing it's not an OHAttributedLabel issue but a problem understanding the way AutoLayout works and probably some inconsistency in your AutoLayout constraints.

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

2 participants