[英]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];
如果你想输出如下
一
二
三
四
五
六
-(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;
}
}
但是如果你想输出为:
一二三
四五六
-(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)];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.