iphoneWindow Based Application
In this tutorial will learn how to create a view without using interface Builder,will create our own view with coding and also add some label on it with background color.To do this will start our project with Window Based Application and in that will add UIViewController subclass without Xib file in Classes file.
Final project will look like this:
Myproject name is ProjectWithoutNib and the ViewController is named as ViewWithoutNib.First will import our view into ProjectWithoutNibAppDelegate.h fileand also class.
Nowwill design our view in ViewWithoutNib.m file, will add all our code to -(void)loadView.First will create view of regular size (320, 480) and will set thebackground color to it and then will place three label of different size andalso different color, will write on it.
Nowadd this to .m file:
Viewwith background color(red) and with regular Frame(0, 0, 320, 480).
self.view = [[UIViewalloc] initWithFrame:CGRectMake(0,0, 320, 480)];
self.view.backgroundColor = [UIColorredColor];
ViewIn View we are adding three different labels with different frame, font, size and alignment.
Creating variable for label on view and frame area.
UILabel *myLabel = [[UILabelalloc] initWithFrame:CGRectMake(80,80, 200, 100)];
Insertingtext into label and alignment of text into label
myLabel.text = @"Hi Ankur ";
myLabel.textAlignment = UITextAlignmentCenter;
Forfont type and size of font, also for label background color
myLabel.font = [UIFont fontWithName:@"Zapfino"size:20];
myLabel.backgroundColor = [UIColor colorWithRed:0.2green:0.4 blue:0.1alpha:0.2];
UILabel *myLabel2 = [[UILabelalloc] initWithFrame:CGRectMake(50,200, 200, 80)];
myLabel2.text = @" Mishra ";
myLabel2.textAlignment = UITextAlignmentLeft; // is for alignment
Textcolor which is printed on label and font type with size
myLabel2.textColor = [UIColor orangeColor];
myLabel2.font = [UIFont fontWithName:@"Zapfino"size:25];
myLabel2.backgroundColor = [UIColor yellowColor];// different function for setting background label color.
UILabel *myLabel1 = [[UILabelalloc] initWithFrame:CGRectMake(50,300, 180, 90)];
myLabel1.text = @" How are you ";
myLabel1.textAlignment = UITextAlignmentRight; // is for alignment
myLabel1.backgroundColor = [UIColor colorWithRed:0.1green:0.9 blue:0.2alpha:0.4];
Herewe are adding all our labels to the view which we created by using[self.view addSubview:labelname] and after that we are releasing all ourvariables(label) even view.
Afteradding all Press Build And Go.
Posted on: September 5, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles