0

I want to add UILabel controls to my application dynamically from code (programmatically), please help me

pratik
  • 4,419
  • 8
  • 41
  • 65

1 Answers1

4

You can do it the following way in one of the view controller methods:

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(...)]; // Create UILabel
myLabel.tag = someTag; // set label's tag to access this label in future via [view viewWithTag:someTag];
... // set label's properties like text, background and text color, font size etc (e.g. myLabel.textColor = [UIColor redColor];)
[view addSubView:myLabel]; // add label to your view
[myLabel release]; // view owns the label now so we can release it
Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • is it possible for all kind of controls? (adding complete functionality of controls) i am trying to achive dynamically loading of controls based on XML fetched from server. – Ante Sep 24 '10 at 23:52
  • @Ante, sure you can set all properties for any control programmatically – Vladimir Sep 25 '10 at 10:07
  • can you give me code example of dynamically created button that onclick event changes some dynamically created text label? – Ante Sep 28 '10 at 21:01
  • @Ante, better create separate question for that. Or search SO - there's a number of similar questions already (e.g. http://stackoverflow.com/questions/1378765/how-do-i-create-a-basic-uibutton-programmatically) – Vladimir Sep 29 '10 at 07:34