在模拟器中旋转iPad时,如何设置标签框

时间:2011-08-23 09:52:57

标签: iphone objective-c ipad

我是iPad开发的新手。在这里,我有一个动态内容的tableview。每个单元格包含两个标签,第二个标签可以增加或减少。最后我将标签添加到单元格中。但是当我旋转模拟器时,单元格中的标签不会改变。我无法为标签设置框架。请帮我。

提前致谢。

UILabel *label1 = nil;
UILabel *label2 = nil;

NSLog(@"it decides the tablecell");
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];

    label1 =[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 44) ];
    //label1 = [[UILabel alloc] initWithFrame:CGRectZero];
    [label1 setLineBreakMode:UILineBreakModeWordWrap];
    [label1 setMinimumFontSize:FONT_SIZE];
    [label1 setNumberOfLines:0];
    [label1 setFont:[UIFont boldSystemFontOfSize:FONT_SIZE]];


    label2 = [[UILabel alloc] initWithFrame:CGRectZero];
    [label2 setMinimumFontSize:FONT_SIZE];
    [label2 setNumberOfLines:0];
    [label2 setLineBreakMode:UILineBreakModeWordWrap];
    [label2 setFont:[UIFont systemFontOfSize:FONT_SIZE]];


}
NSString *text1 = [arrOutline1 objectAtIndex:[indexPath row]];
NSString *text2 = [arrOutline2 objectAtIndex:[indexPath row]];



CGSize constraint = CGSizeMake((CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2))/ 2, 20000.0f);

CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGSize size2 = [text2 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];


[label1 setText:text1];
[label2 setText:text2];

    [label1 setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN-10,( CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2))/2, MAX(size1.height, 44.0f))];

NSLog(@"now it is in cell methd");
UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation == UIDeviceOrientationLandscapeLeft||orientation==UIDeviceOrientationLandscapeRight)
{
    [label2 setFrame:CGRectMake(300 , CELL_CONTENT_MARGIN - 10,500 , MAX(size2.height, 44.0f))];
    NSLog(@"orientation is :LAndscape");
    //[[cell contentView] addSubview:label2];
    [cell addSubview:label2];

}
else
{
    NSLog(@"orientation is Portrait.");

    [label2 setFrame:CGRectMake(300 , CELL_CONTENT_MARGIN - 10,200 , MAX(size2.height, 44.0f))];
    [cell addSubview:label2];


}
    //[label2 setFrame:CGRectMake(300 , CELL_CONTENT_MARGIN - 10,( CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2)) /2 , MAX(size2.height, 44.0f))];




cellHeight =size2.height + 44;

label1.backgroundColor =[UIColor clearColor];
label2.backgroundColor =[UIColor clearColor];


[[cell contentView] addSubview:label1];
[label1 release];
[label2 release];

return cell;

}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

[TableOutline reloadData];
return YES;
}

1 个答案:

答案 0 :(得分:1)

您需要在iPad旋转时自动调整子视图的大小吗?

1)确保您的父视图(在我的情况下,我猜表格视图单元格)将autoresizesSubviews设置为YES(这将是默认值)

2)确保您添加的标签具有为autoresizingMask设置的正确值,例如

// To keep label1 the same size and have label2 stretch to fill the rest of the cell . . .
[label1 setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
[label2 setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

此处,您已将标签1设置为距离左侧固定的距离并且是固定宽度。您已将标签2设置为从左侧和右侧固定的距离,但具有可变宽度。当表格单元格的框架发生变化时,它会自动更改其内部标签的框架。