从 UIView 中的 NSMutableArray 垂直显示 UILabel

Display the UILabel verticallly from the NSMutableArray inside the UIView

我有一个数组对象列表,我将其传递给循环并将其动态显示为 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) createlabel:(CGRect) 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 中而不是在控制器中使用此方法。

按照@Larme 提到的更改您的 y 坐标

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

如果你想要输出如下

one
two
three
four
five
six

-(void) createlabel:(CGRect) 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) createlabel:(CGRect) 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)];