Reputation: 1184
I have a list of array objects which I pass into to a loop and display it as UILabel
dynamically. However I need the values to display it in a vertical. Below are my code,
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)];
This displays me as
one two three four five six
but i need it as
one two three
four five six
can anyone suggest me how can i achieve it? Note: I use this method inside the UIView not in the controller.
Upvotes: 2
Views: 69
Reputation: 2520
Change your y-coordinate as @Larme mentioned
labelArrays = [NSMutableArray arrayWithObjects:@"One”, @“Two”, @“Three", @"four", @"five", @"six" ,nil];
If you want output as following
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;
}
}
But if you want output as :
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;
}
}
}
Function call
[self createlabel:CGRectMake(25,130,100,40)];
Upvotes: 1