Possible attributedText issue
Closed this issue · 2 comments
ArquintL commented
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];
}
...
}
cbess commented
Please make a pull request, so I can test it.