• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 从 UIView 内的 NSMutableArray 垂直显示 UILabel

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:28:10 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个数组对象列表,我将其传递给循环并将其动态显示为 UILabel。但是我需要这些值以垂直显示它。以下是我的代码,

 labelArrays = [NSMutableArray arrayWithObjects"One”, @“Two”, @“Three",@"four",@"five",@"six" ,nil];

//inside a method with a loop am passing the array and creating the labels.
-(void) createlabelCGRect) frame{
  float xCoordinate = frame.origin.x;
  float yCoordinate = frame.origin.y;
  int labelHeight =30;
  int gapBetweenTwoLabels =2;
  int labelWidth = 100;
  for(int counter = 0; counter < [labelArrays count]; counter++){
  UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(xCoordinate,yCoordinate,labelWidth,labelHeight)];
  label.text = [labelArrays objectAtIndex:counter];
  [self addSubview:label];
  xCoordinate = xCoordinate + labelWidth + gapBetweenTwoLabels;
 }
}
//frame
[self createlabel:CGRectMake(25,130,100,40)];

这显示我为

one two three four five six

但我需要它

one two three
four five six

谁能建议我如何实现它?注意:我在 UIView 中使用此方法,而不是在 Controller 中。



Best Answer-推荐答案


按照@Larme 所述更改您的 y 坐标

labelArrays = [NSMutableArray arrayWithObjects"One”, @“Two”, @“Three", @"four", @"five", @"six" ,nil];

如果你想输出如下

one
two
three
four
five
six

-(void) createlabelCGRect) frame {
    CGFloat xCoordinate = frame.origin.x;
    CGFloat yCoordinate = frame.origin.y;
    CGFloat labelHeight = frame.size.height;
    CGFloat labelWidth = frame.size.width;
    CGFloat gapBetweenTwoLabels = 2;

    for(int counter = 0; counter < [labelArrays count]; counter++) {
        UILabel *label = [[UILabel alloc]initWithFrame : CGRectMake(xCoordinate, yCoordinate, labelWidth, labelHeight)];
        label.text = [labelArrays objectAtIndex:counter];
        [self addSubview:label];
        yCoordinate = yCoordinate + labelHeight + gapBetweenTwoLabels;
    }
}

但如果你想输出为:

one two three
four five six

-(void) createlabelCGRect) frame {
    CGFloat xCoordinate = frame.origin.x;
    CGFloat yCoordinate = frame.origin.y;
    CGFloat labelHeight = frame.size.height;
    CGFloat labelWidth = frame.size.width;
    CGFloat gapBetweenTwoLabels = 2;

    for(int counter = 0; counter < [labelArrays count]; counter++) {
        UILabel *label = [[UILabel alloc]initWithFrame : CGRectMake(xCoordinate, yCoordinate, labelWidth, labelHeight)];
        label.text = [labelArrays objectAtIndex:counter];
        [self addSubview:label];
        if((counter+1)%3 == 0) {
            xCoordinate = 0;
            yCoordinate = yCoordinate + labelHeight + gapBetweenTwoLabels;
        } else {
            xCoordinate = xCoordinate + labelWidth + gapBetweenTwoLabels;
        }
    }
}

函数调用

[self createlabel:CGRectMake(25,130,100,40)];

关于ios - 从 UIView 内的 NSMutableArray 垂直显示 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31706578/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap