动态改变UILabel高度bug

时间:2012-10-25 22:10:07

标签: iphone ios xcode uilabel

我正在根据文字更改UILabel高度。它工作得很好,但是如果字体大小超过19.0f,我发现只有一个案例不会加载。如果我改变它是没关系的,但我给了用户选项来改变字体大小,所以......

它开始重叠视图,弄乱视图,这里是示例: enter image description here

以下是我用来调整标签大小的代码:

-(float)resizeToFit{
    float height = [self expectedHeight];
    CGRect newFrame = [self frame];
    newFrame.size.height = height;
    [self setFrame:newFrame];
    return newFrame.origin.y + newFrame.size.height;
}

-(float)expectedHeight{
    [self setNumberOfLines:0];
    [self setLineBreakMode:UILineBreakModeWordWrap];

    CGSize maximumLabelSize = CGSizeMake(self.frame.size.width,9999);

    CGSize expectedLabelSize = [[self text] sizeWithFont:[self font] 
                                            constrainedToSize:maximumLabelSize
                                            lineBreakMode:[self lineBreakMode]]; 
    return expectedLabelSize.height;
}

谢谢!

0 个答案:

没有答案