将 sizeThatFits: 和 sizeToFit 与 UILabel 一起使用的正确方法是什么?
目前,我有一个使用 [aString sizeWithFont:constrainedToSize:lineBreakMode:] 正确调整标签大小的标签,但我已将旋转引入到我的设备中,并且具有灵活的宽度和高度,这会导致我的 UILabel 拉伸宽度方式(因为它的设置相对于主视图),但不会收缩高度以补偿额外的空间。
在我问的另一个问题中,我被告知使用 sizeToFit
和/或 sizeThatFits:
但是我在互联网上找不到任何有用的资源来告诉我如何正确使用它我所做的试验导致了非理性的行为。
简而言之,假设我有一个这样的结构:
UIView *innerView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, 0)];
[innerView setAutoresizingMask:*flexible width and height*]
[innerView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:innerView];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, innerView.frame.size.width-20, 0)];
[myLabel setAutoresizingMask:*flexible width and height*]
[myLabel setBackgroundColor:[UIColor blueColor]];
[myLabel setNumberOfLines:0];
[myLabel setLineBreakMode:UILineBreakModeWordWrap];
[innerView addSubview:myLabel];
这是一个简化版本(也是免责声明,这不是我的实际代码,我意识到这些元素会泄漏以及其他所有内容......这只是一个示例,所以你可以看到结构)。
基本上我有这两个要素。现在,在旋转时,它们都会拉伸以适应新的视图尺寸。我需要的是一种增加或减少高度以动态适应内容的方法。 [aString sizeWithFont...]
只是静态的。那么我如何使用 sizeToFit
或 sizeThatFits
来使其动态化?
谢谢。
汤姆
At the moment I have a label being sized correctly using [aString sizeWithFont:constrainedToSize:lineBreakMode:] but I've introduced rotation into my device and with a flexible width and height this results in my UILabel stretching width ways (because its set relative to the main view) but not then contracting height-wise to compensate for the extra space.
In another question I asked I was told to use sizeToFit
and/or sizeThatFits:
however I can't find any useful resource on the internet that tells me how to use this appropriately and trials I've done result in irrational behavior.
In a nutshell, lets say I have a structure like this:
UIView *innerView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, 0)];
[innerView setAutoresizingMask:*flexible width and height*]
[innerView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:innerView];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, innerView.frame.size.width-20, 0)];
[myLabel setAutoresizingMask:*flexible width and height*]
[myLabel setBackgroundColor:[UIColor blueColor]];
[myLabel setNumberOfLines:0];
[myLabel setLineBreakMode:UILineBreakModeWordWrap];
[innerView addSubview:myLabel];
Thats a simplified version (also by way of disclaimer, this isn't my acctual code, I realise these elements will leak and everything else... this is just an example so you can see the structure).
Basically I have those two elements. Now on rotation they will both stretch to fit the new view size. What I need is a way to increase or decrease the height to fit the content dynamically. [aString sizeWithFont...]
is only static. So how would I use sizeToFit
or sizeThatFits
to make this dynamic?
Thank you.
Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过在屏幕旋转后重置标签的框架?检查interfaceOrientation的值:
然后,为didRotateFromInterfaceOrientation内的每种视图模式相应地设置标签的框架。
以下是根据字符串长度调整标签框架大小的示例: http://cocoamatic.blogspot.com/2010/08/uilabel-dynamic-sizing-based-on-string.html
Have you tried resetting the frame of your labels after the screen rotation? Check the value of interfaceOrientation in:
Then, set the label's frame accordingly for each view mode inside didRotateFromInterfaceOrientation.
Here's an example of resizing label frames for string length: http://cocoamatic.blogspot.com/2010/08/uilabel-dynamic-sizing-based-on-string.html