Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add labels and text boxes programmatically in iPhone

How can I add labels and text boxes in iphone programmatically? How can I set frame for the labels and text boxes?

like image 327
sravan kumar Avatar asked Jan 28 '11 12:01

sravan kumar


1 Answers

The following code creates a UILabel and a UITextField and adds them to the view controllers view. Add this code in the loadView method or somewhere in your view controller.

// Create Label
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 200, 40)];
[myLabel setBackgroundColor:[UIColor clearColor]];
[myLabel setText:@"Hi Label"];
[[self view] addSubview:myLabel];
[myLabel release];

// Create Text Field
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 200, 40)];
[myTextField setBackgroundColor:[UIColor clearColor]];
[myTextField setText:@"Hi Text Field"];
[[self view] addSubview:myTextField];
[myTextField release];

You can also set other properties using the setter methods.

like image 99
EmptyStack Avatar answered Sep 19 '22 00:09

EmptyStack