How can I add labels and text boxes in iphone programmatically? How can I set frame for the labels and text boxes?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With