1

我有一个由代码制作的 TextView。

CGRect textFrame = CGRectMake(5, 20, 300, 50);

UITextView *textView = [[UITextView alloc] initWithFrame:textFrame];

问题是,我想在调用横向模式时调整它的大小。

这里有些例子:

https://docs.google.com/file/d/0BzsX_FHtJa9nSjRVamtFX1pjdlE/edit?usp=sharing

https://docs.google.com/file/d/0BzsX_FHtJa9nU0U5T3JvZkgzZWc/edit?usp=sharing

谢谢

PS:这仅适用于iPhone。没有 iPad 实施。

编辑:

表视图代码:

tableProducts.dataSource = self;
tableProducts.delegate = self;
tableProducts.bounces = YES;

单元格代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableProducts dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;        
}

/* ------ Product title in RED ------*/

CGRect titleFrame = CGRectMake(5, 5, 250, 15);
UILabel *productTitle = [[UILabel alloc] initWithFrame:titleFrame];
productTitle.font = [UIFont boldSystemFontOfSize:14];
productTitle.textColor = [UIColor redColor];
productTitle.text = [listProducts objectAtIndex:indexPath.row];

[cell.contentView addSubview:productTitle];

/* ------ Product title in RED ------*/

/* ------ Product Text ------*/

CGRect textFrame = CGRectMake(5, 20, 300, 50);
UITextView *textView = [[UITextView alloc] initWithFrame:textFrame];
textView.textColor = [UIColor blackColor];
textView.font = [UIFont boldSystemFontOfSize:11.5];
textView.text = [listTexts objectAtIndex:indexPath.row];
textView.scrollEnabled = NO;
textView.editable = NO;

[cell.contentView addSubview:textView];

/* ------ Product Text ------*/

return cell;
}
4

2 回答 2

1

只需在切换方向时更改框架。为 shouldAutoRotate 返回 yes,然后在 willAnimateRotation 中执行以下操作:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
    {
       textView.frame = CGRectMake(x,y,width,height)//whatever values you want for landscape
    }
    else
    {
       textView.frame = CGRectMake(x,y,width,height)//whatever values you want for portrait
    }
}
于 2013-02-07T22:48:46.027 回答
1

设置文本的自动调整大小掩码

 textview.autoresizingMask = UIViewAutoresizingFlexibleWidth;
于 2013-02-07T22:26:41.843 回答