sizeToFit is functioning oddly

梦想与她 提交于 2019-12-06 03:46:33
Jessedc

In order to determine the size that your label will need to be, you need to look into the NSString size methods. There's a number of them that can tell you the size a string will be given a font, and various constraints.

For example [@"string" sizeWithFont: forWidth: lineBreakMode:]; will return a CGSize that you can then use to size your label appropriately.

As an aside, I find the UILabel does not handle strings with multiple lines well. You may be better off writing your own subview of UIView that can handle multiple lines of text, each with its own label.

It seems to happen each time the frame is resized after the initial operation. Presumably the frame's current size is used when deciding the new size in -sizeToFit. The workaround I'm using is to set the view's frame to a pre-defined size before each call to sizeToFit, e.g:

myLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.width.height);
[myLabel sizeToFit];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!