iOS text box Basic use, and the role of all proxy methods

Source: Internet
Author: User

/*

Uitextfield Text Input Box

*/

Uitextfield * TextField = [[Uitextfield alloc]initwithframe:cgrectmake (50, 50, 275, 50)];

Set Border form

/*

Uitextborderstyleroundedrect fillet Form

Uitextborderstyleline Line Form

Uitextborderstylebezel Groove Form

*/

Textfield.borderstyle = Uitextborderstyleroundedrect;

Typically used to look for text displayed in the current text input box

Textfield.text = @ "";

Text color

Textfield.textcolor = [Uicolor Redcolor];

Set Text font size

Textfield.font = [Uifont systemfontofsize:20];

Set Background color

Textfield.backgroundcolor = [Uicolor Lightgraycolor];

Clear text when you start editing repeatedly

textfield.clearsonbeginediting = YES;

Text hints

Textfield.placeholder = @ "Please enter your region name";

Text ciphertext (dark text) This property is typically used to set the password entry box

Textfield.securetextentry = NO;

Alignment of text input

Textfield.textalignment = Nstextalignmentcenter;

Clear button for text input

/*

Uitextfieldviewmodewhileediting when input

Uitextfieldviewmodealways always

Uitextfieldviewmodeunlessediting not in the input time

*/

Textfield.clearbuttonmode = uitextfieldviewmodewhileediting;

Type of keyboard

Textfield.keyboardtype = Uikeyboardtypedefault;

Retuan key types to customize the keyboard

Textfield.returnkeytype = Uireturnkeyjoin;

Left view

UILabel * label = [[UILabel alloc]initwithframe:cgrectmake (0, 0, 50, 50)];

Label.text = @ "Account";

Label.textalignment = Nstextalignmentcenter;

Textfield.leftview = label;

Textfield.leftviewmode = uitextfieldviewmodewhileediting;

Right view

UIButton * button = [UIButton buttonwithtype:uibuttontyperoundedrect];

[Button settitle:@ "OK" forstate:uicontrolstatenormal];

Button.frame = CGRectMake (0, 0, 50, 50);

[Button addtarget:self action: @selector (ButtonClick:) forcontrolevents:uicontroleventtouchupinside];

Textfield.rightview = button;

Textfield.rightviewmode = Uitextfieldviewmodealways;

[Self.window Addsubview:textfield];

Let the keyboard produce the first response the keyboard will bounce automatically

[TextField Becomefirstresponder];

Pick up the keyboard

/*

1. Click the return key of the keyboard

2. Click button

3. Click the blank space to bounce back to the keyboard

*/

  

/*

Gesture

*/

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

Self-customizing Method/Gesture Method

-(void) tapclick{

Uitextfield * TextField = (uitextfield*) [Self.window viewwithtag:100];

[TextField Resignfirstresponder];

}

-(void) ButtonClick: (uibutton*) button{

Cancel First Response

Uitextfield * textfiled = (uitextfield*) [Self.window viewwithtag:100];

[Textfiled Resignfirstresponder];

}

All Proxy methods function

Called when the return key is clicked is usually used to retract the keyboard

-(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{

[TextField Resignfirstresponder];

return yes;//5.1 before setting No to click Invalid

}

Called when the text input box starts typing

-(void) textfielddidbeginediting: (Uitextfield *) textfield{

Eject the keyboard

NSLog (@ "Start input");

}

Called when the text input box finishes typing

-(void) textfielddidendediting: (Uitextfield *) textfield{

Gets the text entered in the current text input box

NSLog (@ "input is:%@", Textfield.text);

Example: Determine if the account writing form is correct if incorrect prompt to fill in the wrong re-enter

NSLog (@ "End input");

}

The method that is called when the content of the text input box changes

-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) string{

/*

NSLog (@ "content:%@", textfield.text);//Get what you entered last time

NSLog (@ "Location:%lu length:%lu", range.location,range.length);//range is the position of the current text, the length is zero

NSLog (@ "==%@==", string);//Get the current input character in real time

*/

Need to get all the text in the current text box in real time

NSString * ResultStr = [Textfield.text stringbyappendingstring:string];

NSLog (@ "%@", resultstr);

You can determine whether the input text is correct in this method

return YES;

}

Know

Allow text input box to enter

-(BOOL) textfieldshouldbeginediting: (Uitextfield *) textfield{

return YES;

}

Allow text input box to end

-(BOOL) textfieldshouldendediting: (Uitextfield *) textfield{

In this method, you can limit whether the keyboard can be retracted by judging the length of the text

return NO;

}

Whether it is allowed to be cleared

-(BOOL) Textfieldshouldclear: (Uitextfield *) textfield{

NSLog (@ "text is cleared");

return YES;

}

iOS text box Basic use, and the role of all proxy methods

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.