gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:13:43 25 4
gpt4 key购买 nike

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

最佳答案

按照@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)];

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com