无法在横向模式下增加 UILabel 的宽度
我制作了一个iPad应用程序,其中我有tableView,其名称为crollTableView,在我的tableView中,我在行的每个单元格中创建两个标签,其名称为capitalLabel和stateLabel,在纵向模式下,
现在我想增加我的宽度横向模式下的标签,
这是代码片段,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView==crollTableView){
static NSString *CellIdentifier=@"Cell";
static NSInteger StateTag = 1;
static NSInteger CapitalTag = 2;
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
// cell.backgroundColor=[UIColor yellowColor];
cell=[[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]autorelease];
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size.height = 70;
frame.size.width = 650;
UILabel *capitalLabel = [[UILabel alloc] initWithFrame:frame];
capitalLabel.tag = CapitalTag;
capitalLabel.opaque = FALSE;
capitalLabel.font=[UIFont systemFontOfSize:20.0];
[cell.contentView addSubview:capitalLabel];
frame.origin.x += 680;
UILabel *stateLabel = [[UILabel alloc] initWithFrame:frame];
stateLabel.tag = StateTag;
stateLabel.font=[UIFont systemFontOfSize:17.0];
[cell.contentView addSubview:stateLabel];
}
UILabel* capitalLabel = (UILabel *) [cell.contentView viewWithTag:CapitalTag];
UILabel* stateLabel = (UILabel *) [cell.contentView viewWithTag:StateTag];
capitalLabel.text=[news2 objectAtIndex:indexPath.row];
stateLabel.text = [news3 objectAtIndex:indexPath.row];
capitalLabel.backgroundColor = [UIColor clearColor];
stateLabel.backgroundColor = [UIColor clearColor];
capitalLabel.textColor=[UIColor whiteColor];
stateLabel.textColor=[UIColor whiteColor];
if(count<[news3 count]){
capitalLabel.numberOfLines = 0;
[capitalLabel sizeToFit];
stateLabel.numberOfLines = 0;
[stateLabel sizeToFit];
}count++;
return cell;
}
}
我应该在代码中进行哪些更改?
I had made an iPad application, in that I have tableView whose name is crollTableView, in my tableView I am creating two labels in each cell of row, whose name is capitalLabel and stateLabel, in portrait mode,
now I want to increase width of my labels in landscape mode,
here is code snippet,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView==crollTableView){
static NSString *CellIdentifier=@"Cell";
static NSInteger StateTag = 1;
static NSInteger CapitalTag = 2;
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
// cell.backgroundColor=[UIColor yellowColor];
cell=[[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]autorelease];
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size.height = 70;
frame.size.width = 650;
UILabel *capitalLabel = [[UILabel alloc] initWithFrame:frame];
capitalLabel.tag = CapitalTag;
capitalLabel.opaque = FALSE;
capitalLabel.font=[UIFont systemFontOfSize:20.0];
[cell.contentView addSubview:capitalLabel];
frame.origin.x += 680;
UILabel *stateLabel = [[UILabel alloc] initWithFrame:frame];
stateLabel.tag = StateTag;
stateLabel.font=[UIFont systemFontOfSize:17.0];
[cell.contentView addSubview:stateLabel];
}
UILabel* capitalLabel = (UILabel *) [cell.contentView viewWithTag:CapitalTag];
UILabel* stateLabel = (UILabel *) [cell.contentView viewWithTag:StateTag];
capitalLabel.text=[news2 objectAtIndex:indexPath.row];
stateLabel.text = [news3 objectAtIndex:indexPath.row];
capitalLabel.backgroundColor = [UIColor clearColor];
stateLabel.backgroundColor = [UIColor clearColor];
capitalLabel.textColor=[UIColor whiteColor];
stateLabel.textColor=[UIColor whiteColor];
if(count<[news3 count]){
capitalLabel.numberOfLines = 0;
[capitalLabel sizeToFit];
stateLabel.numberOfLines = 0;
[stateLabel sizeToFit];
}count++;
return cell;
}
}
what changes should I do in my code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该覆盖
通过检查当前方向是横向还是纵向的条件,您可以相应地设置标签的框架。
You should override
By checking the condition whether the current orientation is landscape or portrait you can set the frame of labels accordingly.
尝试将
autoresizingMask
设置为要自动缩放的视图:在标签的
alloc+init
之后的if (cell == nil)
中添加:Try to set
autoresizingMask
to views that you want to automatically scale:In
if (cell == nil)
afteralloc+init
of labels add:在iOS中,更改方向时不关心增加任何控件的宽度..只需将2个标签的自动调整大小掩码属性设置为UIViewAutoresizingFlexibleWidth..这意味着控件的宽度将根据到超级视图帧大小。
in iOS dont care about increase the width for the any control when change the orientation .. just set the autoresizing mask property for the 2 lables to be UIViewAutoresizingFlexibleWidth .. that means the width of your control will change according to the superview frame size.
尝试设置
autoResizingMask
,以便视图在方向改变时自动调整。您可以更改 Xcode 大小检查器中的值。
Try to Set
autoResizingMask
so that view will automatically adjust itself during orientation change.You can change the value in
Xcode
size inspector.