iOS Development Learning Note 019-dynamic creation of controls

Source: Internet
Author: User

dynamically creating ControlsOne, ButtonSecond, text input boxThird, lable label

Attention:

Just a simple drag-and-drop control will ruin you, so it's best to manually create the control from the code.

If you want to generate a button through code, you can implement it in the system's own function Viewdidload. You should have one class for each control, so you can create an object directly from the class, that is, a control, and then gradually set the properties of the control.

The following operations are basically generic and operate basically the same under different controlsOne, Button 1, create the object, this is nothing to say

UIButton *btn = [[UIButton alloc] init];

2. Also set some basic properties before displaying to the view controller

1.1 Set Button normal state properties

Set a background picture

UIImage *image = [UIImage imagenamed:@ "Btn_01.png"];

[btn setbackgroundimage:image forstate:uicontrolstatenormal];

Set the displayed text

[Btn settitle: @ "Come on" Forstate:uicontrolstatenormal];

Set Text color

Uicolor *color = [Uicolor colorwithred:0 green:0 blue:124 alpha:1];

[btn settitlecolor:color forstate:uicontrolstatenormal];

UIControlStateNormal, uicontrolstatehighlighted represents the state of the button, indicating normal mode and highlight mode

1.2 Setting the attribute in the highlight state

Image = [UIImage imagenamed:@ "Btn_02.png"];

[Btn Setbackgroundimage:image forstate:uicontrolstatehighlighted];

Set the displayed text

[Btn settitle:@ "to Die" forstate:uicontrolstatehighlighted];

Set Text color

color = [Uicolor colorwithred:200 green:0 blue:0 alpha:1];

[Btn Settitlecolor:color forstate:uicontrolstatehighlighted];

3, after the creation of the object to add to the view, so that it can be displayed.

[Self.view ADDSUBVIEW:BTN]; Add a button to the Viwe in the controller

See the code for the exact process.

1 #import "SLQViewController.h"2 3 Const intDELTA =5;4 @interfaceSlqviewcontroller ()5 6 @end7 8 @implementationSlqviewcontroller9 Ten //The controller's view is loaded and is called once One- (void) Viewdidload A { - [Super Viewdidload]; -     //additional setup after loading the view, typically from a nib. theNSLog (@"viewdidload-----"); -      -     //1. Create a button -UIButton *btn =[[UIButton alloc] init]; +      -     //setting location and dimensions +Btn.frame = CGRectMake (0,0, -, -); A     //Set button normal state properties at     //set a background picture -UIImage *image = [UIImage imagenamed:@"Btn_01.png"]; - [btn setbackgroundimage:image forstate:uicontrolstatenormal]; -     //set the displayed text -[BTN Settitle:@"come on"Forstate:uicontrolstatenormal]; -     //Set Text color inUicolor *color = [Uicolor colorwithred:0Green0Blue124Alpha1]; - [btn Settitlecolor:color forstate:uicontrolstatenormal]; to     //; +     //[btn settitlecolor:<# (Uicolor *) #> forstate:<# (uicontrolstate) #>]; -      the     //set the attribute in the highlight state *Image = [UIImage imagenamed:@"Btn_02.png"]; $ [btn setbackgroundimage:image forstate:uicontrolstatehighlighted];Panax Notoginseng     //set the displayed text -[BTN Settitle:@"to Die"forstate:uicontrolstatehighlighted]; the     //Set Text color +color = [Uicolor colorwithred: $Green0Blue0Alpha1]; A [btn Settitlecolor:color forstate:uicontrolstatehighlighted]; the      +      -      $     //Monitor button click events $ [btn addtarget:self Action: @selector (Btnclick:) forcontrolevents:uicontroleventtouchupinside]; -     //2, add the button to the controller in the Viwe - [Self.view addsubview:btn]; the } - Wuyi- (void) didreceivememorywarning the { - [Super didreceivememorywarning]; Wu     //Dispose of any resources the can be recreated. - } About  $  -- (void) Btnclick: (UIButton *) btn - { -NSLog (@"Click button:%p", BTN); A}
Second, text input boxCreate a text input box

     1  text box 2     //  Create object 3     text.frame = CGRectMake (  - 0  -  - // setting location and dimensions 4     // Set Background color 5     // add to view controller's views

Effect so

Third, lable labelCreate lable Tags
1    //lable Label2UILabel *lable = [[UILabel alloc] init];//Create a Lable object3Lable.frame = CGRectMake ( -, -, -, -);//setting location and dimensions4Lable.text =@"This is a lable";//Set display text5Lable.textcolor = [Uicolor colorwithred:255Green0Blue0Alpha1];//Setting the RGBA value6Lable.font = [Uifont systemfontofsize:Ten];7[Self.view addsubview:lable];//Add to view

The properties and methods contained in this control can be found directly in the source file. For example, the following operational properties of lable are available.

1@property (nonatomic,copy) NSString *text;//default is nil2@property (Nonatomic,retain) Uifont *font;//default is nil (System font Plain)3@property (Nonatomic,retain) Uicolor *textcolor;//default is nil (text draws black)4@property (Nonatomic,retain) Uicolor *shadowcolor;//default is nil (no shadow)5@property (nonatomic) cgsize shadowoffset;//default is Cgsizemake (0,-1)--a top shadow6@property (nonatomic) nstextalignment textalignment;//default is Nstextalignmentleft7@property (nonatomic) Nslinebreakmode Linebreakmode;//default is Nslinebreakbytruncatingtail.

Summarize:

The general process for creating controls through code is

1, the creation of an OC class object, is actually corresponding to a control

For example UIButton *btn = [[UIButton alloc] init]; //

2, set the general properties of the control, otherwise cannot be displayed in the view

For example, set the location size, title, font, background and other properties, you can go to the corresponding class to see the properties and methods can be used.

3. Add to view view so that it will eventually appear in the program.

iOS Development Learning Note 019-dynamic creation of controls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.