2010-10-26 26 views
3

目前我有一个使用[aString sizeWithFont:constrainedToSize:lineBreakMode:]正确调整大小的标签,但是我已经将旋转引入了我的设备,并且具有灵活的宽度和高度,这导致了我的UILabel拉伸宽度方式(因为它的集合相对于主视图),但不是然后收缩高度来补偿额外的空间。使用sizeThatFits和sizeToFit与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]; 

那是一个简化版本(也声明的方式,这不是我的acctual代码,我意识到这些元素会泄漏和一切......这只是一个例子,所以你可以看到结构)。

基本上我有这两个元素。现在旋转时,它们都会伸展以适合新的视图尺寸。我需要的是一种增加或减少高度以适应动态内容的方式。 [aString sizeWithFont...]只是静态的。那么我将如何使用sizeToFitsizeThatFits来实现这种动态?

谢谢。

Tom

回答

2

您是否尝试过在屏幕旋转后重置标签的框架?检查的interfaceOrientation值中:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 

然后,对于每个视图模式相应地设置标签的框架内部didRotateFromInterfaceOrientation

下面是一个为字符串长度调整标签帧大小的示例:http://cocoamatic.blogspot.com/2010/08/uilabel-dynamic-sizing-based-on-string.html

相关问题