Why can't I change the frame of my UILabel?

♀尐吖头ヾ 提交于 2019-12-03 12:09:13

If you're using auto layout, you shouldn't be doing any setting of frames -- either turn off auto layout, or use constraints to change the size of your label. The easiest way to do this is to give your label a height constraint (and width too, if you want a constant width) in IB, and make an IBOutlet to it. Then, in code, change the constant value of that constraint based on the value you get from sizeWithFont:constrainedToSize:lineBreakMode:.

Your label has a zero frame to begin with, so, when you do this:

CGSize textSize = [self.descriptionLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0]
                                         constrainedToSize:CGSizeMake(self.descriptionLabel.frame.size.width, FLT_MAX)
                                             lineBreakMode:self.descriptionLabel.lineBreakMode];

You're asking the sizeWithFont method to do something effectively impossible - tell you the height that is required when no width is allowed. The response it provides (33525.000000) is probably the height if one character is allowed on each line or something like that.

So, either set the width of the label before you start, or provide the allowed width to the sizeWithFont method and then set the label size using the response (not just the height).

Also, consider what you're trying to do by changing the bounds. Comment that code out till you get the frame right at least, then you can add it back in and check it actually does what you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!