0

我在表格视图单元格中有一个标签,我必须根据标签中的文本调整其大小。在 potratit 模式下它可以正常工作,但是当切换到横向模式时,文本不会显示到表格单元格的长度。目前我已经动态地创建了标签,并通过重新加载表格来根据设备的当前方向调整其宽度。

我已经动态创建了标签并将其添加到 contentView 中。代码如下

UILabel *lblSize = [[UILabel alloc] initWithFrame:CGRectMake(nameLbl.frame.origin.x + nameLbl.frame.size.width , 0, 580, 60)];
lblSize.backgroundColor = [UIColor clearColor];
lblSize.font = [UIFont fontWithName:@"TrebuchetMS" size:18];
lblSize.textColor = [UIColor blackColor];
lblSize.minimumFontSize = 10;
lblSize.tag = 3;
[cell.contentView addSubview:lblSize];

当我以纵向模式执行代码时会发生什么如下

|第1页| 一些文字。一些文字。一些文字。一些文字。一些文字.... |

横向模式下发生的情况如下

|第1页| 一些文字。一些文字。.... |

我不想使用我使用过的方法。有没有其他更合适的方法可以用来解决问题。请提出一些解决方案。我目前正在使用 iPad,需要一个解决方案。

4

2 回答 2

0

只需使用以下条件设置框架

    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
    {
         // code for landscape orientation      
    }

或者

    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
    {
         // code for Portrait orientation       
    }
于 2012-11-29T06:57:12.477 回答
0

在你的使用以下cellForRowAtIndexPath

CGFloat maxHeightForLabel=1000.0;
CGSize size=[label.text sizeWithFont:label.font
       constrainedToSize:CGSizeMake(label.frame.size.width, maxHeightForLabel)];
CGRect frame=label.frame;
frame.size=size;
label.frame=frame;

呼吁[yourTableView reloadData];改变方向。

于 2012-11-29T07:27:44.393 回答