无法在横向模式下增加 UILabel 的宽度

发布于 2024-12-27 16:14:52 字数 2260 浏览 3 评论 0原文

我制作了一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

要走干脆点 2025-01-03 16:14:52

您应该覆盖

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }

通过检查当前方向是横向还是纵向的条件,您可以相应地设置标签的框架。

You should override

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }

By checking the condition whether the current orientation is landscape or portrait you can set the frame of labels accordingly.

情话已封尘 2025-01-03 16:14:52

尝试将 autoresizingMask 设置为要自动缩放的视图:

在标签的 alloc+init 之后的 if (cell == nil) 中添加:

capitalLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

stateLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

Try to set autoresizingMask to views that you want to automatically scale:

In if (cell == nil) after alloc+init of labels add:

capitalLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

stateLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
红ご颜醉 2025-01-03 16:14:52

在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.

飞烟轻若梦 2025-01-03 16:14:52

尝试设置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.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文