cbess/AutoScrollLabel

Possible attributedText issue

Closed this issue · 2 comments

Update: Sorry I was wrong. I'm using CBAutoScrollLabel with attributed strings, but [attributedText size] returns a smaller width than [text sizeWithFont:], so that the label is to short for my string. As long as the attributedText has the same font size as the text property, there isn't an issue.

If self.mainLabel.attributedText contains a different text than self.mainLabel.text or if the text is styled differently (e.g. different font size), the label size gets wrong calculated.
Here's my fix (in CBAutoScrollLabel.m):
If the text property gets set, set the attributedText nil and vice versa:

 - (void)setText:(NSString *)theText refreshLabels:(BOOL)refresh
 {
     ...
     EACH_LABEL(text, theText)
     EACH_LABEL(attributedText, nil)
     ...
}

 - (void)setAttributedText:(NSAttributedString *)theText refreshLabels:(BOOL)refresh
 {
     ...
     EACH_LABEL(attributedText, theText)
     EACH_LABEL(text, nil)
     ...
}

And calculate the label size depending on which property is set:

 - (void) refreshLabels
 {
      ...
      // calculate the label size
      CGSize labelSize = CGSizeZero;

      if (self.mainLabel.text != nil)
      {
           labelSize = [self.mainLabel.text sizeWithFont:self.mainLabel.font
                                   constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.bounds))];
      }
      else  if (self.mainLabel.attributedText != nil)
      {
           labelSize = [self.mainLabel.attributedText size];
      }
      ...
 }

Please make a pull request, so I can test it.

@cbess thanks for your answer, but I don't understand what CBSize [attributedText size] returns, because in my case the returned width is to less to fit.
Also my if statement "self.mainLabel.text != nil" doesn't work as intended