iOS create UILabels dynamically - ios

Sometimes I want my view to contain 5 UILabels, sometimes 3 and sometimes n.
The number of UILabels depends on data that's fetched from a website.

You'll have to make them in code instead of interface builder
for (int i = 0; i < n; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(/* where you want it*/)];
label.text = #"text"; //etc...
[self.view addSubview:label];
[label release];
}

A generic answer for a generic question:
while (labelsToDisplay)
{
UILabel *label = [[UILabel alloc] initWithFrame:aFrame];
[label setText:#"someText"];
[aViewContainer addSubview:label];
[label release];
}

NSArray *dataArray;
float xCoordinate=10.0,yCoordinate=10.0,width=100,height=40;
float ver_space=20.0;
for (int i = 0; i <dataArray.count; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(xCoordinate,yCoordinate,width,height)];
label.text = [dataArray objectAtIndex:i];
[self.view addSubview:label];
yCoordinate=yCoordinate+height+ver_space;
}

UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(125, 12,170,20)];
lbl.text=#"IOS";
lbl.textAlignment = NSTextAlignmentCenter;
lbl.textColor = [UIColor whiteColor];
lbl.font = [UIFont fontWithName:#"AlNile" size:10.0];
lbl.backgroundColor=[[UIColor redColor]colorWithAlphaComponent:0.5f];
lbl.layer.borderColor=[UIColor blackColor].CGColor;
lbl.layer.borderWidth=1.0f;
lbl.layer.cornerRadius = 6.0f;
[self.view addSubview:lbl];

UILabel *lblTitle=[[UILabel alloc]init];
[lblTitle setFrame:CGRectMake(0, 0, 100, 100)];
[lblTitle setText:#"MAK"];
[lblTitle setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:lblTitle];
-Here UILable will be created dynamically.
-but property will be set differently.

Create a TextView to show the text on the labels and a NSArray to contain the data.
For more information:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html
http://developer.apple.com/library/ios/documentation/uikit/reference/UITextView_Class/Reference/UITextView.html

Related

Generated subview is always behind image view

viewController.m
-(void) viewDidLoad
{
[super viewDidLoad];
[self addScoreRowViewWithIndexLeft:(++i) andName:[scoreDict objectForKey:#"name"] andScore:[[scoreDict objectForKey:#"points"] integerValue]];
}
-(void) addScoreRowViewWithIndexLeft:(NSInteger)index andName: (NSString *)name andScore: (NSInteger)score {
UIView *scoreRowView = [[UIView alloc] initWithFrame:CGRectMake(128, 112 + index * 80, 280, 20)];
UILabel *indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 20, 20)];
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 20, 180, 20)];
UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(330, 20, 50, 20)];
UIColor *color = [UIColor darkGrayColor];
[indexLabel setText:[#((int)index) stringValue]];
[indexLabel setFont:[UIFont fontWithName:#"DINAlternate-Bold" size:18]];
[indexLabel setTextColor:color];
[nameLabel setText:name];
[nameLabel setFont:[UIFont fontWithName:#"DINAlternate-Bold" size:18]];
[nameLabel setTextColor:color];
[scoreLabel setText:[#((int)score) stringValue]];
[scoreLabel setFont:[UIFont fontWithName:#"DINAlternate-Bold" size:18]];
[scoreLabel setTextColor:color];
[scoreRowView addSubview:indexLabel];
[scoreRowView addSubview:nameLabel];
[scoreRowView addSubview:scoreLabel];
[self.view addSubview:scoreRowView];
}
I added my ImageView in my Storyboard as a subview of my View.
When I delete the ImageView, everything is ok. I get displayed my other code generated subviews. But when I add the ImageView in my Storyboard, it's on top of my other subviews. If I give my ImageView an alpha of 0.3 I can see my subviews shining through.
What's wrong with my code? I also tried;
[self.view bringSubviewToFront:scoreRowView];
but it doesn't work.
What can I do?
Try to change zPosition of your ImageView and scoreRowView. You can do this this way:
Connect your ImageView with your ViewController
In bottom of ViewDidLoad add something like that:
yourImageView.layer.zPosition = 1;
scoreRowView.layer.zPosition = 2;
I'm suspecting that [self.view bringSubviewToFront:scoreRowView]; doesn't work because you invoke it in ViewDidLoad where view in not fully work. You can try to invoke it in e.g. ViewDidAppear and check if that will work.

How to add multiple UILabels programmatically in Xcode?

Hello in Xcode I am trying to add two labels to my image programmatically but when I add the second one it will overwrite my first one. Here is my code:
//first label
UILabel* caption = [[UILabel alloc] initWithFrame:
CGRectMake(-25, 0, 320, 50)
];
caption.backgroundColor = [UIColor whiteColor];
caption.textColor = [UIColor blackColor];
caption.textAlignment = UITextAlignmentLeft;
caption.font = [UIFont systemFontOfSize:16];
caption.text = [NSString stringWithFormat:#" #%#",[data objectForKey:#"username"]];
[self addSubview: caption];
//second label
UILabel* bottomBox = [[UILabel alloc] initWithFrame:
CGRectMake(-25, -200, 320, 50)
];
caption.backgroundColor = [UIColor whiteColor];
caption.textColor = [UIColor blackColor];
caption.textAlignment = UITextAlignmentCenter;
caption.font = [UIFont systemFontOfSize:16];
caption.text = [NSString stringWithFormat:#" Testing"];
[self addSubview: bottomBox];
I've tried changing the "0 to -200 to modify the Y coordinates so that the second label will shift down but it just overwrites the first one for some reason. Any help would be greatly appreciated thanks.
All the code for your 2nd label is referencing the 1st variable caption instead of bottomBox. You want:
//second label
UILabel* bottomBox = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 320, 50)];
bottomBox.backgroundColor = [UIColor whiteColor];
bottomBox.textColor = [UIColor blackColor];
bottomBox.textAlignment = UITextAlignmentCenter;
bottomBox.font = [UIFont systemFontOfSize:16];
bottomBox.text = #"Testing";
[self addSubview: bottomBox];

Uiscrollview not able to add uilabels

I am trying to add uilabels to a uiscrollview. I use a for loop to create 20 labels and then add them one by one in the scroll view. Does no work. Here is my code
-(void)setScrollViews{
for (int i = 0; i < 80; i++)
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 20 * i,160 , 30)];
label.font = [UIFont systemFontOfSize:12.0];
label.textColor = [UIColor blueColor];
label.text = [NSString stringWithFormat:#"Label number %d", i ];
label.numberOfLines = 0;
[label sizeToFit];
[self.activitiesScrollView setContentSize:CGSizeMake(320, self.activitiesScrollView.frame.size.height + 20)];
[self.activitiesScrollView addSubview:label];
[self.view addSubview:self.activitiesScrollView];
}
}
I am using storyboard. I also tried alloc init but it still failed. I did set the delegate in the viewdidload. I changed the background image for the scrollview to check if it is there on the screen and it is.
EDIT:This works now, All I did was restart Xcode
try this:
-(void)setScrollViews{
for (int i = 0; i < 80; i++)
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 20 * i,160 , 30)];
label.font = [UIFont systemFontOfSize:12.0];
label.textColor = [UIColor blueColor];
label.text = [NSString stringWithFormat:#"Label number %d", i ];
label.numberOfLines = 0;
[label sizeToFit];
[self.activitiesScrollView setContentSize:CGSizeMake(320, self.activitiesScrollView.frame.size.height + 20)];
[self.activitiesScrollView addSubview:label];
}
[self.view addSubview:self.activitiesScrollView];
}
try this . . .
you are adding scrollview 20 times to the uiview in the loop . that is also wrong part of your code
-(void)setScrollViews{
[self.view addSubview:self.activitiesScrollView];
for (int i = 0; i < 80; i++)
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 20 * i,160 , 30)];
label.font = [UIFont systemFontOfSize:12.0];
label.textColor = [UIColor blueColor];
label.text = [NSString stringWithFormat:#"Label number %d", i ];
label.numberOfLines = 0;
[label sizeToFit];
[self.activitiesScrollView addSubview:label];
[self.activitiesScrollView setContentSize:CGSizeMake(320, self.activitiesScrollView.frame.size.height + 20)];
}
}

setFrame of UILabel property of .xib doesn´t work

i make the next method in a viewController
-(void)setupDocsLabel:(NSMutableArray *)documents{
self.lDocumentos.frame = CGRectMake(self.lDocumentos.frame.origin.x, kFirstLabelYPosition+actualLabelYPos,self.lDocumentos.frame.size.width,lDocumentos.frame.size.height);
self.Documentos.frame = CGRectMake(self.Documentos.frame.origin.x, kFirstLabelYPosition+actualLabelYPos,self.Documentos.frame.size.width,self.Documentos.frame.size.height);
actualLabelYPos +=20.0;
for (DocInformation *doc in documents) {
NSString *textLabel = [doc.documentDescription stringByAppendingString:#" :"];
UIFont *lblFont = lDocumentos.font;
CGSize sizeFont = [textLabel sizeWithFont:lblFont forWidth:120.0 lineBreakMode:NSLineBreakByTruncatingTail];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(lDocumentos.frame.origin.x+20, kFirstLabelYPosition+actualLabelYPos,sizeFont.width,sizeFont.height)] retain];
label.text = textLabel;
[label setFont:lblFont];
[label setTextColor:lDocumentos.textColor];
[label setBackgroundColor:[UIColor clearColor]];
//[label setLineBreakMode:NSLineBreakByTruncatingTail];
NSString *textDoc = doc.cdgoDocum;
UIFont *lblFontDoc = Documentos.font;
CGSize sizeFontDoc = [textDoc sizeWithFont:lblFontDoc];
UILabel *labelDoc = [[[UILabel alloc] initWithFrame:CGRectMake(label.frame.origin.x+label.frame.size.width+20, kFirstLabelYPosition+actualLabelYPos,sizeFontDoc.width,lDocumentos.frame.size.height)] retain];
labelDoc.text = textDoc;
[labelDoc setFont:lblFontDoc];
[labelDoc setTextColor:lDocumentos.textColor];
[labelDoc setBackgroundColor:[UIColor clearColor]];
[self.scrollView addSubview:label];
[self.scrollView addSubview:labelDoc];
[label release];
[labelDoc release];
actualLabelYPos+=20.0;
}
//[self.view setNeedsDisplay];
//[self.view setNeedsLayout];
}
New Labels add to the scrollView well, but self.lDocumentos (a IBOutlet of the .xib) don´t change his position.
Thanks for the help!!
I've solved it, the problem was that the .xib had enabled Use AutoLayout in the File Inspector.

How can I know which view I am clicking? And why the button no reaction?

I am designing a viewcontroller which have several button bar, each bar canbe clicked and show a content view.like:
http://i.stack.imgur.com/m5V4Q.png
When I click the buttonbar, it's frame will expand bigger, and you can see the content in it(which is a button).
First, a bar button(320*30 size) and a contentView is a set, they are subviews of listview. And several listview makes the whole view.
Second, when I click it, the list view will expand from 320*30 to 320*180, the contentview in it will expand from 300*0 to 300 * 130(20 pixel of margin). and I set the clipsToBounds of contentview to Yes to make sure the content in contentview won't show when the frame height is 0.
Now,the click can show the contentview exactly as I want. But here is a problem: I can't click the button in it, and I tried to set their userInteractionEnabled to yes . And even I set the contentview as user-defined,which I set the view's userInteractionEnabled to yes and overwrite the touchbegin function to show a alertView. Those test are failed, no reaction found. And I checked and sure that there shouldn't be any view covered it.
What's the reason might be?
The code of setup the list view is:
NSArray *array = [titleArray objectAtIndex:i];
NSString *ShenSuoTitle = [array objectAtIndex:0];
NSString *ShenSuoContent = [array objectAtIndex:1];
UIView *listView = [[UIView alloc] initWithFrame:CGRectMake(0, totalHeight, 320, ShenSuoViewHeight)];
[listView setBackgroundColor:[UIColor blackColor]];
[listView setTag:[[NSString stringWithFormat:#"%d",(i+1)] intValue]];
[self.scrollView addSubview:listView];
totalHeight = totalHeight + 1 + ShenSuoViewHeight;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[titleView setBackgroundColor:[UIColor whiteColor]];
[titleView setTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
[listView addSubview:titleView];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:[[NSString stringWithFormat:#"%d2",i+1] intValue]];
[btn setFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[btn addTarget:self action:#selector(reSetFrame:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:btn];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(5, 12, 30, 30)];
img.image = [UIImage imageNamed:#"list_ico.png"];
[img setTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
[titleView addSubview:img];
NSLog(#"img:%f,%f",img.frame.origin.y,img.frame.size.height);
UILabel *labTitle = [[UILabel alloc] initWithFrame:CGRectMake(35, 15, 100, 25)];
labTitle.textColor = [UIColor blackColor];
labTitle.backgroundColor = [UIColor clearColor];
labTitle.font = [UIFont boldSystemFontOfSize:15];
labTitle.text = ShenSuoTitle;
[titleView addSubview:labTitle];
NSLog(#"labTitle:%f,%f",labTitle.frame.origin.y,labTitle.frame.size.height);
//add a label for selected
UILabel *selectedLabel = [[UILabel alloc] initWithFrame:CGRectMake(214, 14, 86, 21)];
selectedLabel.textColor = [UIColor grayColor];
//selectedLabel.alpha = 0.75;
selectedLabel.backgroundColor = [UIColor clearColor];
selectedLabel.text = #"All";
selectedLabel.font = [UIFont boldSystemFontOfSize:15];
[selectedLabel setTag:[[NSString stringWithFormat:#"%d5",i+1] intValue]];
[titleView addSubview:selectedLabel];
NSLog(#"selectedLabel:%f,%f",selectedLabel.frame.origin.y,selectedLabel.frame.size.height);
UIView *content = [[UIView alloc] initWithFrame:CGRectMake(10, 10 + ShenSuoViewHeight, 300, 0)];
content.backgroundColor = [UIColor grayColor];
UILabel *testLa = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
testLa.text = #"Label";
UIButton *testBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testBut.frame = CGRectMake(0, 40, 100, 30);
testBut.titleLabel.textColor = [UIColor greenColor];
testBut.titleLabel.text = #"Button";
content.tag = [[NSString stringWithFormat:#"%d4",i+1] intValue];
[content addSubview:testLa];
[content addSubview:testBut];
content.clipsToBounds = YES;
[testLa release];
[listView addSubview:content];
[content release];
[labTitle release];
[img release];
[titleView release];
[listView release];
the code handle the click to make the list view expands is:
UIView *titleView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
titleView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:0.9];
UIView *listView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d",i+1] intValue]];
listView.frame = CGRectMake(0, totalHeight, 320, ShenSuoViewHeight);
totalHeight = totalHeight + 1 + 20 + 180 + ShenSuoViewHeight;
UIView *content = [self.view viewWithTag:[[NSString stringWithFormat:#"%d4",i+1] intValue]];
content.frame = CGRectMake(10, 10 + ShenSuoViewHeight, 300, 180);
UIImageView *img = (UIImageView*)[self.view viewWithTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
img.image = [UIImage imageNamed:#"list_ico_d.png"];
I'm not sure I follow your situation exactly, and some code would be nice. To answer your question though, the reason is most likely that the touch is being intercepted elsewhere. It may also be that you just forgot to connect an action method to the button. Try using breakpoints or NSLogs to see what methods are being triggered and which aren't.
Thanks a lot. It is due to my fault. The code of expanding the bar which setting the listview's frame is wrong, it should add in the expanded height. This leads the visual is ok but the clicking not be handled.
So this problem closed.

Resources