UIButton, UILabel, Uitextfield beginners need to understand the basic definitions and common settings

Source: Internet
Author: User
Tags set background

The following are the most commonly used controls in iOS development, and as part of iOS basic Learning tutorial knowledge, beginners need to understand their basic definitions and common settings in order to be proficient in the development of three.

UIButton button

  First, the definition of UIButton

UIButton *button=[[uibutton Buttonwithtype: (Uibuttontype);

There are 6 kinds of button types that can be defined:

typedef enum {

Uibuttontypecustom = 0, custom style

Uibuttontyperoundedrect, Rounded Rectangle

Uibuttontypedetaildisclosure, Blue small arrow button, mainly to do detailed instructions with

Uibuttontypeinfolight, bright exclamation marks

Uibuttontypeinfodark, Dark exclamation

Uibuttontypecontactadd, Cross plus button

} Uibuttontype;

  Second, set frame

Button1.frame = CGRectMake (20, 20, 280, 40);

[Button Setframe:cgrectmake (20,20,50,50)];

  Third, button background color

Button1.backgroundcolor = [Uicolor Clearcolor];

[Button Setbackgroundcolor:[uicolor Bluecolor];

  Iv. State Status

Forstate: The function of this parameter is to define how the text or picture of the button will appear in the state.

enum {

UIControlStateNormal = 0, normal state appearance

uicontrolstatehighlighted = 1 << 0, highlight state appears

uicontrolstatedisabled = 1 << 1, disabled status only appears

uicontrolstateselected = 1 << 2, check status

Uicontrolstateapplication = 0x00ff0000 When the application is marked

uicontrolstatereserved = 0xff000000 is reserved for internal frames and can be used regardless of

};

@property (nonatomic,getter=isenabled) BOOL enabled; Default is YES. If NO, ignores touch events and subclasses may draw differently

@property (nonatomic,getter=isselected) BOOL selected; Default is NO could used by some subclasses or by application

@property (nonatomic,getter=ishighlighted) BOOL highlighted;

  Set button to fill picture and background picture

[buttonsetimage:[uiimageimagenamed:@ "Checkmarkcontrollericon"]forstate:uicontrolstatenormal];

[buttonsetbackgroundimage:[uiimageimagenamed:@ "Checkmarkcontrollericon"]forstate:uicontrolstatenormal];

  Six, set the button title and title color

[Button1 Settitle: @ "click" Forstate:uicontrolstatenormal];

[Buttonsettitlecolor:[uicolorredcolor]forstate:uicontrolstatenormal];

  Seven, the setting button will glow when pressed

Button.showstouchwhenhighlighted=no;

 Eighth, add or remove event handling

[Button1 addtarget:self Action: @selector (Butclick:) forcontrolevents:uicontroleventtouchupinside];

[Btn Removetarget:nil Action:nil forcontrolevents:uicontroleventtouchupinside];

  Ninth, set button interior picture spacing and title spacing

Uiedgeinsets insets; Set the internal picture spacing of the button

Insets.top = Insets.bottom = Insets.right = Insets.left = 10;

Bt.contentedgeinsets = insets;

Bt.titleedgeinsets = insets; Title Spacing

Tenth, other

The Set button is an invalid button, and if the button is invalid, the button will no longer respond to the user.

btn.enabled = YES;

Add a gesture recognizer to a button

[Btn Addgesturerecognizer:tap];

//Add a button, example

UIButton *calbtn = [[UIButton alloc]initwithframe:cgrectmake (50, 200, 200, 40)]; Button size

Calbtn.backgroundcolor = [Uicolor Orangecolor]; Background color

[Calbtn settitle:@ "Point me, I calculate" forstate:uicontrolstatenormal]; Set text in default state

[Calbtn settitle:@ "Point me, I calculate" forstate:uicontrolstatehighlighted]; Set the text in the highlighted state

[Calbtn setbackgroundimage:[uiimage imagenamed:@ "Login_btn_n_normal"] forstate:uicontrolstatenormal]; Set a background picture by default

[Calbtn setbackgroundimage:[uiimage imagenamed:@ "logoff_btn_n_highlighted"] forstate:uicontrolstatehighlighted]; Set the background picture in the highlighted state

[Self.view ADDSUBVIEW:CALBTN]; The most important thing is to add a button

" Note " The name of the picture to be modified in advance, it is best to add the following words to distinguish between the default state or the highlighted state of the word

UILabel Label

UILabel *LBL = [[UILabel alloc]initwithframe:cgrectmake (50, 100, 300, 160)]; Size

Lbl.backgroundcolor = [Uicolor Lightgraycolor]; Background color

Lbl.textcolor = [Uicolor Bluecolor]; Font Color

Lbl.shadowcolor = [Uicolor Redcolor]; Shadow effects, not commonly used

Lbl.shadowoffset = Cgsizemake (4,-10);

Lbl.text = @ "Dorm"; Add text

Label Content Alignment

Lbl.textalignment = Nstextalignmentcenter;

Sets the number of rows for the label, if set to 0, indicating that there can be any number of rows

Lbl.numberoflines = 2;

When the label has more than one row, set the line wrapping method, the default is in Word units

Lbl.linebreakmode = Nslinebreakbytruncatingmiddle; If not fully displayed, there will be three dots in the middle.

Set the label highlighting state

lbl.highlighted = YES;

Set the font color when the label is highlighted

Lbl.highlightedtextcolor = [Uicolor Purplecolor];

Allow users to interact with tags

lbl.userinteractionenabled = YES; Allow user interaction

Define a click Gesture Recognizer Object

UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initwithtarget:self Action: @selector (lblclicked:)];

Add a gesture recognizer to the label

[LbL Addgesturerecognizer:tap];

lbl.enabled = NO;

Lbl.adjustsfontsizetofitwidth = YES;

Lbl.baselineadjustment = uibaselineadjustmentaligncenters;

[Self.view ADDSUBVIEW:LBL]; The control finally needs to be added

"Summary" under the people can try to use,?? Practice is the only standard to test truth

1.text: Sets the label display text.

2.attributedText: Sets the Label property text.

3.font: Sets the label text font.

4.textAlignment: Sets the label text alignment.

5.lineBreakMode: Set the way the label text is displayed too long, this property is used in the word wrapping and truncation of the Chinese label. First, the NumberOfLines must be set to 0 to have an effect.

6.enabled: Sets whether the text content is variable.

7.adjustsFontSizeToFitWidth: Text content Adaptive label width.

8.adjustsLetterSpacingToFitWidth: Adapts the width of the label according to the spacing of the letters, beyond the part to ... Show.

9.numberOfLines: Labels display up to a maximum number of rows.

10.minimumScaleFactor: Set the minimum font, same as minimumfontsize, Minimumfontsize is not available after iOS 6.

11.highlightedTextColor: Set the text highlighting color to use with highlighted.

12.shadowColor: Sets the text shadow color.

13.shadowColor: Sets the offset of the text shadow from the original text. Label.shadowoffset = Cgsizemake (1.0, 5.0);
14.userInteractionEnabled: Sets whether the label ignores or removes user interaction. The default is No.

15.preferredMaxLayoutWidth: Prioritizes the maximum width of the label layout.

16.baselineAdjustment: If the Adjustsfontsizetofitwidth property is set to Yes, this property controls the behavior of the text baseline.

BackgroundColor background Color

Uitextfield Text

1.enablesReturnKeyAutomatically

The default is no, and if set to Yes, no characters are entered in the text box, the back button in the lower right corner of the keyboard is disabled.

2.borderStyle

Set border style to show border style only if set

Text.borderstyle = Uitextborderstyleroundedrect;

typedef enum {

Uitextborderstylenone,

Uitextborderstyleline,

Uitextborderstylebezel,

Uitextborderstyleroundedrect

} Uitextborderstyle;

3.backgroundColor

Sets the background color of the input box, which is set to white if you use a custom background picture border is ignored

Text.backgroundcolor = [Uicolor Whitecolor];

4.background

Set background

Text.background = [UIImage imagenamed:@ "Xx.png"]; Uitextfield background, note that only uitextborderstylenone when the property is valid

Set the Enable to no when the background of the TextField

Text.disabledbackground = [UIImage imagenamed: @ "Ff.png"];

5.placeholder

When the input box does not have content, the prompt content is password

Text.placeholder = @ "password"; You can call him a placeholder.

6.font

Set font style and size for input box contents

Text.font = [Uifont fontwithname:@ "Arial" size:20.0f];

7. TextColor

Set Font Color

Text.textcolor = [Uicolor Redcolor];

8. Clearbuttonmode

Whether there is a fork in the input box, when it appears, to delete the contents of the input box at once

Text.clearbuttonmode = Uitextfieldviewmodealways;

typedef enum {

Uitextfieldviewmodenever, the heavy does not appear

Uitextfieldviewmodewhileediting, appearing when editing

Uitextfieldviewmodeunlessediting, except for the editor's appearance.

Uitextfieldviewmodealways always appears.

} Uitextfieldviewmode;

9. Text

Enter the text from the beginning of the box

Text.text = @ "The beginning of the text in the input box";

Ten. Securetextentry

This property is set when each character is entered into a point to enter the password.

Text.securetextentry = YES; Dark text

Clearsonbeginediting.

Edit it again and empty it.

text.clearsonbeginediting = YES;

TextAlignment.

Content Alignment

Text.textalignment = Uitextalignmentleft;

Contentverticalalignment.

The vertical alignment of content Uitextfield inherits from Uicontrol, which has an attribute in this class contentverticalalignment

Text.contentverticalalignment = Uicontrolcontentverticalalignmentcenter;

Adjustsfontsizetofitwidth.

When set to Yes, the text automatically shrinks to fit the text window size. The default is to keep the original size and let long text scroll

Textfied.adjustsfontsizetofitwidth = YES;

Set the minimum font size for auto-shrink display

Text.minimumfontsize = 20;

Keyboardtype.

Set the keyboard style

Text.keyboardtype = Uikeyboardtypenumberpad;

typedef enum {

Uikeyboardtypedefault, default keyboard, all characters supported

Uikeyboardtypeasciicapable, default keyboard that supports ASCII

Uikeyboardtypenumbersandpunctuation, standard telephone keypad, supports +*# characters

Uikeyboardtypeurl, URL keyboard, support for. com buttons only support URL characters

Uikeyboardtypenumberpad, Digital keypad

Uikeyboardtypephonepad, telephone keypad

Uikeyboardtypenamephonepad, telephone keypad, also support input person name

Uikeyboardtypeemailaddress, keyboard for entering e-mail addresses

Uikeyboardtypedecimalpad, numeric keypad with numbers and decimal points

Uikeyboardtypetwitter, optimized keyboard for easy input @, #字符

Uikeyboardtypealphabet = uikeyboardtypeasciicapable,

} Uikeyboardtype;

16.autocapitalizationType

Whether the first letter is capitalized

Text.autocapitalizationtype = Uitextautocapitalizationtypenone;

typedef enum {

Uitextautocapitalizationtypenone, not automatically capitalized

Uitextautocapitalizationtypewords, capitalize the first letter of the word

Uitextautocapitalizationtypesentences, capitalize the first letter of the sentence

Uitextautocapitalizationtypeallcharacters, all letters are capitalized.

} Uitextautocapitalizationtype;

Returnkeytype.

Key to what color the return key becomes

Text.returnkeytype =uireturnkeydone;

typedef enum {

Uireturnkeydefault, default Gray button, marked with return

Uireturnkeygo, the blue button labeled Go

Uireturnkeygoogle, a blue button labeled Google, search by language

Uireturnkeyjoin, blue button labeled Join

Uireturnkeynext, the blue button labeled next.

Uireturnkeyroute, a blue button labeled route

Uireturnkeysearch, the blue button labeled Search

Uireturnkeysend, the blue button labeled Send

Uireturnkeyyahoo, the blue button labeled Yahoo!

Uireturnkeyyahoo, the blue button labeled Yahoo!

Uireturnkeyemergencycall, emergency call button

} Uireturnkeytype;

Keyboardappearance.

Keyboard appearance

Textview.keyboardappearance=uikeyboardappearancedefault;

typedef enum {

Uikeyboardappearancedefault, default appearance, light grey

Uikeyboardappearancealert, dark grey graphite

} Uireturnkeytype;

Rightview.

The rightmost plus picture is similar to the left of the following code

Uiimageview *image=[[uiimageview alloc] initwithimage:[uiimage imagenamed:@ "Right.png"];

Text.rightview=image;

Text.rightviewmode = Uitextfieldviewmodealways;

typedef enum {

Uitextfieldviewmodenever,

Uitextfieldviewmodewhileediting,

Uitextfieldviewmodeunlessediting,

Uitextfieldviewmodealways

} Uitextfieldviewmode;

editing .

Whether edits are allowed.

In 21.Stroyboard:

1. Text: Sets the default text for the text box.

2, Placeholder: Can be displayed in the text box gray words, to prompt the user should be in this text box to enter what content. When you enter data in this text box, the gray words for the hint will automatically disappear.

3, Background:

4, Disabled: If this item is selected, the user will not be able to change the text box contents.

5. Next, there are three buttons to set the alignment.

6, Border style: Select the border style.

7, Clear button: This is a drop-down menu, you can choose to clear when the button appears, the so-called Clear button is a text box to the right of the small X, you can have the following options:

7.1 Never appears: Never appears

7.2 appears while editing: appears when editing

7.3 appears unless editing:

7.4 is always visible:

8, clear when editing begins: If this item is selected, the previous contents of the text box will be erased when you start editing the text box. For example, you now enter the text box a "what", then to edit the text box B, if you come back to edit the text box A, then the "what" will be immediately cleared.

9. Text color: Sets the colors of the text in the text box.

10. Font: Sets the font and font size of the text.

11, min Font Size: Sets the minimum font that the text box can display (but I don't feel like it)

12. Adjust to Fit: Specifies whether the text in the text box also shrinks when the size of the text box decreases. Select it to make all text visible, even if the text is long. However, this option is used with the Min font size, and the text shrinks, not less than the set min font size.

The next sections are used to set how the keyboard is displayed.

13, Captitalization: Set uppercase. There are four options in the drop-down menu:

13.1 None: Uppercase not set

13.2 Words: Capitalize the first letter of each word, where the word refers to a string separated by a space

13.3 sentances: The first letter of each sentence is capitalized, and the sentence here is a string separated by a period plus a space

13.4 All characters: so uppercase letters

14, Correction: Check spelling, default is yes.

15, Keyboard: Select the keyboard type, such as full numbers, letters and numbers.

16, Appearance:

17, Return key: Select the Return key, you can choose Search, return, done, and so on.

18, Auto-enable Return key: If selected, the keyboard's return key is valid only if at least one character is entered in the text box.

Add a simple text box

Uitextfield *num1field = [[Uitextfield alloc]initwithframe:cgrectmake (20, 100, 100, 40)]; Size

Num1field.backgroundcolor = [Uicolor Whitecolor]; Background color

Num1field.tag = 110; Tagged tag

Num1field.keyboardtype = Uikeyboardtypenumberpad; Setting up the keyboard

[Self.view Addsubview:num1field]; Add this text box, and the last control needs to be added

" Note " Above summary (some is Baidu's) everybody can try to use,?? Practice is the only standard to test truth

UIButton, UILabel, Uitextfield beginners need to understand the basic definitions and common settings

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.