Let us start creating UIView programmaticaly.
Step 1 Add a UIViewController class, name it YourView.m (and YourView.h).
In YourView.h
UIView *view;
Step 2
In YourView.m un comment loadView.
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
view.backgroundColor = [UIColor yellowColor];
self.view = view;
}
----------------------------------------------------------------------------------------------------------------
Adding a UILabel programmatically
You can use this class to draw one or multiple lines of static text, such as those you might use to identify other parts of your user interface. The base UILabel class provides control over the appearance of your text, including whether it uses a shadow or draws with a highlight. If needed, you can customize the appearance of your text further by subclassing.
UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 200, 80)];
lblName.text = @"Admin";
lblName.textAlignment = UITextAlignmentCenter;
lblName.textColor = [UIColor BlackColor];
lblName.shadowColor = [UIColor whiteColor];
lblName.shadowOffset = CGSizeMake(1,1);
lblName.font = [UIFont fontWithName:@"Zapfino" size:20];
lblName.backgroundColor = [UIColor LightGrey];
[self.view addSubview:lblName];
[lblName release];
----------------------------------------------------------------------------------------------------------------
Text wrapping/ MultiLine
UILabel can display multiple lines of text, and to make it you need to set two properties lineBreakMode and numberOfLines:
lblName.lineBreakMode = UILineBreakModeWordWrap;
lblName.numberOfLines = 2; // 2 lines ; 0 - dynamical number of lines
lblName.text = @"Kamleshwar";
This is all about UILabel. For More your can Refer Developer.Apple.com
----------------------------------------------------------------------------------------------------------------
Adding a UIButton Programmatically
A button intercepts touch events and sends an action message to a target object when tapped. Methods for setting the target and action.
btnSubmit = [[UIButton buttonWithType:UIButtonTypeRoundedRect] initWithFrame:CGRectMake(50, 200, 200, 80) ];
[btnSubmit setTitle:@"Submit" forState:UIControlStateNormal];
[btnSubmit setTitle:@"Done" forState:UIControlEventTouchDown];
[btnSubmit setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btnSubmit.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
btnSubmit.backgroundColor = [UIColor clearColor];
[btnSubmit addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: btnSubmit];
-(void)buttonAction:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"Button Clicked" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
}
If you would like to remove info about what object called the method [(id)sender], remove colon on the end of class name in adding a selector to the button:
[btnSubmit addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
And of Remove: ":(id)sender" from method name
-(void)buttonAction {
}
----------------------------------------------------------------------------------------------------------------
Adding a UITextField Programmatically
UITextField * txtName = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 50)];
txtName.borderStyle = UITextBorderStyleRoundedRect;
txtName.textColor = [UIColor blackColor]; //text color
txtName.font = [UIFont systemFontOfSize:17.0]; //font size
txtName.placeholder = @"Please enter name"; //place holder
txtName.backgroundColor = [UIColor whiteColor]; //background color
txtName.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
txtName.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
txtName.returnKeyType = UIReturnKeyDone; // type of the return key
txtName.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
txtName.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
To Remove Keyboard On Press Done Button
You typically use this UITextField to gather small amounts of text from the user and perform some immediate action, such as a search operation, based on that text.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[txtName resignFirstResponder];
return YES;
}