First, UILabel1, UILabel (label): Is the space to display text. Uilabel is the most frequently occurring control in the app. 2, Uilabel is the subclass of UIView, as subclasses are generally to extend the function of the parent class Uilabel expanded the function of text display, Uilabel is the view that can display the text. 3. Create Uilabel
1 //Create a Uilabel object2UILabel *usernamelabel = [[UILabel alloc] Initwithframe:cgrectmake ( -, -, -, -)];3 4 //Set Text5Usernamelabel.text =@"User name";6 7 //Add Uilabel to Parent view8 [Self.window Addsubview:usernamelabel];9 Ten //Release Ownership (MRC mode) One //[Usernamelabel release]; A //Creating a Uilabel object (adaptation screen) - -UILabel *usernamelabel = [[UILabel alloc] Initwithframe:cgrectmake ( -, $, the, +)]; theUILabel *L1 = [[UILabel alloc] Initwithframe:cgrectmake (Cgrectgetmaxx (usernamelabel.frame) + -, Cgrectgetminy (Usernamelabel.frame), Cgrectgetwidth (Self.window.frame)-Cgrectgetwidth (Usernamelabel.frame)- -, Cgrectgetheight (Usernamelabel.frame))]; -UILabel *L2 = [[UILabel alloc] Initwithframe:cgrectmake (Cgrectgetminx (Usernamelabel.frame), Cgrectgetmaxy ( Usernamelabel.frame) +Ten, Cgrectgetwidth (Usernamelabel.frame), Cgrectgetheight (Usernamelabel.frame))]; -UILabel *l3 = [[UILabel alloc] Initwithframe:cgrectmake (Cgrectgetmaxx (usernamelabel.frame) + -, Cgrectgetmaxy (usernamelabel.frame) +Ten, Cgrectgetwidth (Self.window.frame)-Cgrectgetwidth (Usernamelabel.frame)- -, Cgrectgetheight (Usernamelabel.frame))];
4. Uilabel property setting (Common)
//Set Background colorUsernamelabel.backgroundcolor =[Uicolor Cyancolor]; //Set Text alignmentUsernamelabel.textalignment =Nstextalignmentright; //Set Text colorUsernamelabel.textcolor =[Uicolor Purplecolor]; //Set FontUsernamelabel.font = [Uifont fontwithname:@"Helvetica-bold"Size -]; //Print all font stylesNSLog (@"%@", [Uifont familynames]); //number of rows displayedUsernamelabel.numberoflines =3; //Break mode (word break)Usernamelabel.linebreakmode =nslinebreakbywordwrapping; //Shadow colorUsernamelabel.shadowcolor =[Uicolor Blackcolor]; //Shadow SizeUsernamelabel.shadowoffset = Cgsizemake (2,1);
Second, Uitextfield1, Uitextfield (input box): is to control the text input and display space.
The frequency of Uitextfield in the app is also relatively high. The iOS system uses the virtual keyboard to implement the input, and when you click on the input box, the system automatically brings up the keyboard to facilitate further operation. When you do not need to input, you can use the retract keyboard method to retract the pop-up keyboard. 2, Uitextfield and Uilabel, Uilabel is mainly used for text display, can not be edited, Uitextfield allows users to edit the text (input). 3. Create a Uitextfield object
//Create a Uitextfield objectUitextfield *usernametextfield = [[Uitextfield alloc] Initwithframe:cgrectmake ( -, -, the, -)]; //Set Border StyleUsernametextfield.borderstyle =Uitextborderstyle Roundedrect; //Set PlaceholderUsernametextfield.placeholder =@"mobile phone number/email"; //Add Uitextfield to Parent view[Self.window Addsubview:usernametextfield]; //release Ownership (MRC)[Usernametextfield release];
4. Uitextfield Text Display Properties
// Set text content @" User name: " ; = [Uicolor blackcolor]; // Text Alignment Usernametextfield.textalignment = Nstextalignmentleft;
5. Uitextfield Input Control Properties
// whether to allow editing usernametextfield.enabled = YES; // whether to empty the input box when you start editing usernametextfield.clearsonbeginediting = YES; // is safe input Usernametextfield.securetextentry = YES; // Type of pop -up keyboard Usernametextfield.keyboardtype = Uikeyboardtypealphabet; // The return button type (enumeration value) in the lower right corner of the keyboard Usernametextfield.returnkeytype = Uireturnkeydefault;
6. Uitextfield Appearance Control Properties
//Clear Button ModeUsernametextfield.clearbuttonmode =uitextfieldviewmodewhileediting; //left view of the input boxUIView *leftview = [[UIView alloc] Initwithframe:cgrectmake (0,0, -, -)]; Leftview.backgroundcolor=[Uicolor Yellowcolor]; Usernametextfield.leftview=Leftview; Usernametextfield.leftviewmode=uitextfieldviewmodealways; //right view of the input boxUIView *rightview = [[UIView alloc] Initwithframe:cgrectmake (0,0, -, -)]; Rightview.backgroundcolor=[Uicolor Redcolor]; Usernametextfield.rightview=Rightview; Usernametextfield.rightviewmode= Uitextfieldviewmodealways;
7. Uitextfield Add Recycle keyboard method
//1. Setting up the agentUsernametextfield.Delegate=Self ; //2. Compliance with agreements @interfaceAppdelegate:uiresponder <UIApplicationDelegate,UITextFieldDelegate>//3. Implementing the Protocol approach-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField {NSLog (@"enter key on keyboard"); [TextField Resignfirstresponder]; returnYES; } //become the first responder, run into edit state[Usernametextfield Becomefirstresponder];
7, Uitextfield common Agent method
//1. When TextField will start editing, tell the client-(BOOL) textfieldshouldbeginediting: (Uitextfield *) TextField {returnYES; } //2. When TextField has been edited, tell the client- (void) Textfielddidbeginediting: (Uitextfield *) TextField {}//3. When TextField is about to finish editing, tell the client-(BOOL) textfieldshouldendediting: (Uitextfield *) TextField {returnYES; } //4. When TextField has finished editing, tell the client- (void) Textfielddidendediting: (Uitextfield *) TextField {}//5. When you click the Enter button on the keyboard, tell the client-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField {returnYES; }
Third, UIButton1, UIButton (button): Is the space to respond to user clicks.
UIButton is a very high-frequency control in the app. 2, UIButton and Uilabel, Uitextfield focus on different, focus on dealing with user interaction events. Of course the UIButton class also provides some ways to control the appearance of buttons. 3. Create UIButton, set common Properties
//1. Creating objects and initializing (using class methods)UIButton *button =[UIButton Buttonwithtype:uibuttontypesystem]; //2. Setting PropertiesButton.frame = CGRectMake ( -, -, -, -); Button.backgroundcolor=[Uicolor Orangecolor]; //set the title in normal state[Button Settitle:@"Point Me"Forstate:uicontrolstatenormal]; //set the caption to highlight (click to stay)[Button Settitle:@"who points me"forstate:uicontrolstatehighlighted]; //set the title colorButton.tintcolor =[Uicolor Whitecolor]; //set the background picture of a button//Create a UIImage objectUIImage *afuimage = [UIImage imagenamed:@"afu.jpg"]; //Normal State[button Setbackgroundimage:afuimage Forstate:uicontrolstatenormal]; //In the highlighted state[Button Setbackgroundimage:[uiimage imagenamed:@"zhatian.jpg"] forstate:uicontrolstatehighlighted]; //set the foreground picture (must be a skeleton image)[Button Setimage:[uiimage imagenamed:@"222.png"] Forstate:uicontrolstatenormal]; //3. Add an event//In the click State[button addtarget:self action: @selector (ButtonClick:) forcontrolevents:uicontroleventtouchupinside]; //4. Add to Parent View[Self.window Addsubview:button];
3. Implement Click events
//Implement button click events- (void) ButtonClick: (UIButton *) Sender {NSLog (@"Point Me"); Sender.backgroundcolor= [Uicolor colorwithred:arc4random ()% the/255.0Green:arc4random ()% the/255.0Blue:arc4random ()% the/255.0Alpha1]; //Removing events[Sender removetarget:self Action: @selector (ButtonClick:) forcontrolevents:uicontroleventtouchupinside];}
Iv. Uiimageview1, Uiimageview is the class used in iOS to display pictures, it is equivalent to a picture frame, specifically used as a display picture, can hold a picture or a group of pictures.
2. Create Uiimageview object (add a set of dynamic Picture sample code)
Basics of iOS Learning controls