我有一个由代码制作的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实现。
编辑:
TableView代码:
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;
}
答案 0 :(得分:1)
设置文本的自动调整遮罩
textview.autoresizingMask = UIViewAutoresizingFlexibleWidth;
答案 1 :(得分: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
}
}