AliSoftware / OHAttributedLabel

UILabel that supports NSAttributedString
https://github.com/AliSoftware/OHAttributedLabel/wiki
1.51k stars 344 forks source link

SizeToFit return always 21.0 for height #151

Closed mladjan closed 10 years ago

mladjan commented 11 years ago

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 commented 11 years ago

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 commented 10 years ago

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.