cbess/AutoScrollLabel

Please update your -(void)refreshLabels

Closed this issue · 3 comments

It could be something issue is iOS 7

  • (void)refreshLabels
    {
    __block float offset = 0;

    // calculate the label size
    // CGSize labelSize = [self.mainLabel.text sizeWithFont:self.mainLabel.font
    // constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.bounds))];

UIFont *font = self.mainLabel.font;

if (!self.mainLabel.text) {
    return;
}

NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:self.mainLabel.text attributes:@{NSFontAttributeName:font}];

CGRect rect = [attributedText boundingRectWithSize:(CGSize){CGFLOAT_MAX, CGRectGetHeight(self.mainLabel.bounds)}
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                           context:nil];
CGSize labelSize = rect.size;

Please describe the problem this solves.

I think it's because 'sizeWithFont' was deprecated in iOS 7.

My current hack is:

- (void)refreshLabels {
    __block float offset = 0.0;
    CGSize labelSize;

    // Calculate the label size
    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
        labelSize = [self.mainLabel.text sizeWithAttributes:@{NSFontAttributeName:self.mainLabel.font}];
    }
    else {
        labelSize = [self.mainLabel.text sizeWithFont:self.mainLabel.font constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.bounds))];
    }

    // Round up to nearest whole number
    CGRect integralRect = CGRectZero;
    integralRect.size = labelSize;
    labelSize = CGRectIntegral(integralRect).size;

    each_object(self.labels, ^(UILabel *label) {
        etc…

I hope someone can offer a more robust solution!

Regards,
Andrew

Cool, thanks.