Set name to UILabel as label1, label2 and so on - ios

I want to set name to UILabel as label1,label2,label3 and so on. For that i don't want to write code for label1,label2,... Now i am using this method.
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
.....
But it's not a good method. I only want to write a single line of code for all labels.
How can i implement this? Is it possible to use %d along with label.
Please help.

just set tag of label and get data by using tag
for (int i=0; i<4; i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
label.tag = i;
[self.view addSubview:label];
}

You can use tag of Label to differentiate from other instance. there is no such logic you are looking and it is not suggestible to differentiate objects.
for (int i = 0; i < 4; i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
label.tag = i;
[self.view addSubview:label];
}
if ([self.view viewWithTag:i]) {
[(UILabel *)[self.view viewWithTag:i] setText:#"Update Text"];
}

Related

Objective-C for each item in NSArray create UI Label

I have this block of code here:
for (id object in _theParamenterArray) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 200)];
label.text = #"Custom Label";
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:label];
}
What I am trying to do is create a label for each item in NSArray. My array has 3 rows and each row as the following values: Assemble, Division, Project and WorkOrder and I want to put each value in a UILabel. how would I accomplish this?
I have tried the following:
int counter = 0;
for (id object in _theParamenterArray) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Assemble"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:label];
UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(110, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Division"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelTwo];
UILabel *labelThree = [[UILabel alloc]initWithFrame:CGRectMake(210, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Project"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelThree];
UILabel *labelFour = [[UILabel alloc]initWithFrame:CGRectMake(310, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"WorkOrder"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelFour];
counter++;
}
but it only displays the WorkOrder for all three rows, I am trying to get all values from each row.
Here is a screenshot of my array:
My issue is when I run this code, I only get 1 label that says "Custom Label"
You do get four labels that look like one because they sit on top of each other, and all say the same thing (i.e. "Custom Label").
In order to fix this, you need to do two things:
Make sure that you give your labels different frames - make a counter, decide how far apart the labels should be, and adjust coordinates accordingly, and
Set different text into the labels - for example, use label.text = object (assuming that object is NSString)
The way you adjust the origin of the frame depends on where you want your labels to be in relation to each other. To stack them vertically, use this code:
int counter = 0;
for (id object in _theParamenterArray) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Assemble"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:label];
UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(110, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Division"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelTwo];
UILabel *labelThree = [[UILabel alloc]initWithFrame:CGRectMake(210, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Project"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelThree];
UILabel *labelFour = [[UILabel alloc]initWithFrame:CGRectMake(310, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"WorkOrder"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelFour];
counter++;
}
The 10+30*counter expression places your labels at positions
(10, 10) (110, 10) (210, 10) (310, 10)
(10, 40) (110, 40) (210, 40) (310, 40)
(10, 70) (110, 70) (210, 70) (310, 70)
(10, 100) (110, 100) (210, 100) (310, 100)
You need to increment the Y value like below (Or set the CGRect to be where you want the labels)
NSArray *obj = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", nil];
int x = 200;
for (NSString *object in obj) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, x, 200, 20)];
x+= 40;
label.text = object;
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:label];
}

ObjC - Programmatically add label to UIScrollView

How do you programmaticaly add labels (or other controls for that matter) to a UIScrollView?
I used this code, but I couldn't scroll. Which leads me to believe that it doesn't add the labels to the UIScrollView.
This is my code:
for (int i = 1; i <= 30; i++)
{
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, (i * 50), 200, 40)];
[myLabel setBackgroundColor:[UIColor clearColor]];
[myLabel setText:#"Dynamic"];
[self.Scroller addSubview:myLabel];
}
There are no errors, and it does create the labels, but like I said before, without scrolling.
What are we doing wrong?
Thanks in advance.
[self.Scroller setContentSize:CGSizeMake(self.Scroller.frame.size.width, (30 * 50)];
for (int i = 1; i <= 30; i++)
{
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, (i * 50), 200, 40)];
[myLabel setBackgroundColor:[UIColor clearColor]];
[myLabel setTextColor:[UIColor blackColor]];
[myLabel setText:#"Dynamic"];
[self.Scroller addSubview:myLabel];
}
CGSize newContentSize=scroll.contentSize;
newContentSize.height+=30*50;
[self.Scroller setContentSize:newContentSize];
You are not setting contentSize for scrollView,Add this line in your loop:-
CGSize newContentSize=scroll.contentSize;
newContentSize.height+=i*50;
[self.Scroller setContentSize:newContentSize];

UILabel text won't display

I'm having trouble in an iOS app I'm building with UILabels: despite initializing them the text I programmed won't show up and I'm at wits' end about what I should do.
For example, this is one of the app's screens in which I have this problem:
Welcome.m
#interface Welcome()
#end
#implementation Welcome
-(void) viewdidLoad{
[super viewDidLoad];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 47)];
[self.view addSubview: label];
label.text = #"Benvinguts a la pràctica de iOS I";
label.numberOfLines = 4;
}
-(IBAction) buttonPressed: (id) sender
{
[self performSegueWithIdentifier:#"Tutorial1" sender:self];
}
#end
Could you help me out in fixing this?
You need to add a textColor:
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 47)];
[self.view addSubview: label];
label.text = #"Benvinguts a la pràctica de iOS I";
label.numberOfLines = 4;
label.textColor = [UIColor blackColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 47)];
[self.view addSubview: label];
label.text = #"Benvinguts a la pràctica de iOS I";
label.numberOfLines = 0;
[label SizeToFit];
You may use like this (label size manage by itself.)
Is your Viewcontroller a child? I had the same problem in my code, it would display the box but not ant text. I found a solution by adding a scrollview or something of the sort and adding the label to that got it working.
try this
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 47)];
label.text = #"Benvinguts a la pràctica de iOS I";
label.numberOfLines = 4;
[self.view addSubview: label];
or maybe your label appears under the NavigationController. Try dropping below
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 140, 47)];

self.quickDialogTableView reloadData not reloading data

I have a simple but custom section header in which I am showing dynamic content in a label. However, when I pop a VC and in the active VC I run [self.quickDialogTableView reloadData] in viewWillAppear, the data is not being updated to the correct "self.sectionHeaderTitle. Any idea what I am doing wrong?
(void)sectionHeaderWillAppearForSection:(QSection *)section atIndex:(NSInteger)indexPath {
if (indexPath == 0) {
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 110)];
header.backgroundColor = [UIColor clearColor];
UIView *header_background = [[UIView alloc] initWithFrame:CGRectMake(25, 10, 270, 100)];
header_background.backgroundColor = [UIColor whiteColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 255, 25)];
title.text = #"A sample static title";
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(55, 40, 200, 40)];
label.text = self.sectionHeaderTitle;
[header addSubview:header_background];
[header_background addSubview:title];
[header_background addSubview:label];
[section setHeaderView:header];
}
Try reload the section, like this:
[self.quickDialogTableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationBottom];

How do I add Two UILabels in my UIScrollView for the iphone?

myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
myscrollview.delegate = self;
[self.view addSubview:myscrollview];
CGSize scrollViewContentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+500);
[myscrollview setContentSize:scrollViewContentSize];
lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
lblfirstmsg = [[UILabel alloc] initWithFrame:CGRectMake(15, 50, 295, 800)];
But I don't know My Firstmasg length.... after that lblfirstmsg I want to add second lblsecongmgs.... So how can I add?
After you set first label and set it content you can call [lblfirstmsg sizeToFit]; to fit frame to its content. Then you can add your second UIlabel next to the first one [[UILabel alloc] initWithFrame:CGRectMake(lblfirstmsg.frame.origin.x + lblfirstmsg.frame.size.width, lblfirstmsg.frame.origin.y, 200, 50)];
[myscrollview addSUbview:lblluckyno];
and same way add 2nd label.
This is your scroll view code.
myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
myscrollview.delegate = self;
[self.view addSubview:myscrollview];
[myscrollview setContentSize:CGSizeMake(self.view.frame.size.width,self.view.frame.size.height+500);];
After you set two label With CGRect and add in Scroll View
[myScrollView addSubView:lblluckyno];
......
Hello Mayur P Bhansali,
You can find the position and dimension of your first label like so:
lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
UILabel *secondLabel = [[UILabel alloc] initWithFrame: CGRectMake(lblluckyno.frame.origin.x + lblluckyno.frame.size.width, 200, 100)];
Each UIView has a "frame" object and this frame object has a "origin" C structure and "size" C structure. Using labelName.frame.size.width gives you the width of a frame and labelName.frame.origin.x gives you the X coordinate of the frame.
You could access these properties like this too:
[labelName frame].size.width
[labelName frame].size.origin.x
lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
lblfirstmsg = [[UILabel alloc] initWithFrame:CGRectMake(15, 50, 295, 800)];
[myscrollview addSUbview:lblluckyno];
[myscrollview addSUbview:lblfirstmsg];

Resources