sravan kumar
sravan kumar

Reputation: 61

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?

Upvotes: 6

Views: 20863

Answers (2)

Kasaname
Kasaname

Reputation: 1501

UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 29, 40)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:textField];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 29, 40)];
label.text = @"Custom Label";
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:m_label];

Upvotes: 1

EmptyStack
EmptyStack

Reputation: 51374

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.

Upvotes: 17

Related Questions