Uitextfield text field control-iOS development

Source: Internet
Author: User
Tags uicontrol

Before entering this article, we recommend that you carefully read another blog.:Uicontrol IOS control programming-iOS development http://blog.csdn.net/iukey/article/details/7300412
This will get twice the result with half the effort.

The use of uitextfield is definitely inseparable from the keyboard, so there is also a blog recommendation: uikeyboard keyboard related knowledge point

Why should I read another article about uicontrol? Because uitextfield inherits from the uicontrol class, many attributes, methods, and notifications of uicontrol are applicable to uitextfield, and I will not repeat them here. (It is mainly to reduce the length of the article. It is my goal to understand a knowledge point in the least plain text .)

Create

UITextField* myTextField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];

To create a view with pure code, see my other blog:Http://blog.csdn.net/iukey/article/details/7083165 of several basic knowledge points about View


Set Delegation

Mytextfield. Delegate = self; // The delegate class must comply with the uitextfielddelegate protocol.


Set attributes

The uicontrol attribute is fully usable for uitextfield. the following attributes are extended attributes of uitextfiels:

Mytextfield. textalignment = uitextalignmentleft; // The default value is left alignment, which is the extension property of uitextfield mytextfield. borderstyle = uitextborderstylebezel; // no border by default. If a custom background image border is used, mytextfield is ignored. placeholder = @ "enter your account here"; // draw a gray string for the blank text field as the placeholder mytextfield. clearsonbeginediting = yes; // set to yes. When a text field is clicked, the field content is cleared by mytextfield. adjustsfontsizetofitwidth = yes; // when set to yes, the text will be automatically reduced to adapt to the text window size. By default, the original size is retained, and the long text is scrolled. // mytextfield. Background = [uiimage imagenamed: @ "registbtn"]; // The uiimage object can be accepted. This setting invalidates the border. Mytextfield. clearbuttonmode = quiet; // 'X' Clear button displayed on the right // mytextfield. leftview = // mytextfield. leftviewmode = // mytextfield. rightview = // mytextfield. rightviewmode =

These attributes allow you to attach the derived class of uiview to the left or right of this field. Generally, uibutton objects, such as magnifiers or bookmarks, are attached to text fields. Each attached view has a corresponding mode. You can also set the values of the clearbuttonmode attribute.

Display

 [self.view addSubview:myTextField];


Rewrite painting Behavior

In addition to the style options of the uitextfield object, you can also customize the uitextfield object and add many different rewriting methods for it to change the display behavior of text fields. These methods return a cgrect structure and define the boundary range of each part in the text field. If you create a custom uitextfield class, you can override these methods to change one or more boundaries. Do not directly call fan guangfa; they are all callback functions called by the iPhone Runtime Library. The following is an example:

- (CGRect)clearButtonForBounds:(CGRect)bounds{    return CGRectMake(bounds.origin.x +bounds.size.width-50,                       bounds.origin.y+bounds.size.height-20, 16, 16);}

You can override the following method when creating a uitextfield subclass:

Borderrectforbounds

Rectangle Boundary

Textrectforbounds

Specify the border of the displayed text

Placeholderrectforbounds

Boundary of the specified station text

Editingrectforbounds

Specify the boundaries of text in editing

Clearbuttonrectforbounds

Specify the boundary for displaying the Clear button

Leftviewrectforbounds

Display the border of the left-attached View

Rightviewrectforbounds

Display the border of the right-attached View


Delegate Method

-(Bool) textfieldshouldbeginediting :( uitextfield *) textfield {// return a bool value, specifying whether to start editing return YES For a text field in sequence ;}
-(Void) textfielddidbeginediting :( uitextfield *) textfield {// triggered when editing starts. The text field will become first responder}
-(Bool) textfieldshouldendediting :( uitextfield *) textfield {// return the bool value, specifying whether to allow the text field to end editing. When the editing ends, the text field will let the first responder // to prevent the text field from disappearing when the user finishes editing, you can return no //, which is useful for some programs whose text fields must always be active, for example, the instant message return no ;}
-(Bool) textfield :( uitextfield *) textfield shouldchangecharactersinrange :( nsange) range replacementstring :( nsstring *) string {// when the user uses the automatic correction function, this method is called when the input text is modified to the recommended text. // This is especially useful for applications that want to add revocation options. // you can trace the last modification made in the field, or log all edits for auditing purposes. // To prevent text changes, no can be returned. // The parameter of this method has a nsange object, indicating the location of the changed text. The recommended text also returns yes ;}
-(Bool) textfieldshouldclear :( uitextfield *) textfield {// return a bool value indicating whether to allow content to be cleared based on user requests // you can set the condition to allow content to be cleared. Return yes ;}
-(Bool) textfieldshouldreturn :( uitextfield *) textfield {// return a bool value, specifying whether to allow end editing when the Enter key is pressed // If the resignfirstresponder method is allowed, this leads to the end of editing, and the keyboard will be folded up [textfield resignfirstresponder]; // check the meaning of the resign to understand this method. Return yes ;}

Notification

Uitextfield is derived from uicontrol, so the notification system in the uicontrol class can also be used in text fields. In addition to standard events of the uicontrol class, you can also use the following uitextfield-specific events

Uitextfieldtextdidbegineditingnotification

Uitextfieldtextdidchangenotification

Uitextfieldtextdidendeditingnotification

Triggered when the text field exits the editing mode. The Object Property of the notification stores the final text.

Because text fields use the keyboard to input text, action notifications are also sent when the following events occur.

Uikeyboardwillshownotification

Sent before keyboard display

Uikeyboarddidshownotification

Send after displaying the keyboard

Uikeyboardwillhidenotification

Sent before hiding the keyboard

Uikeyboarddidhidenotification

Send after hidden keyboard

Open the text field on the keyboard.

The pop-up on the keyboard may cover text fields, so we need to move the view up to a certain distance. For more information, see my article to address this issue:

How to solve the problem of hiding view with the keyboard-iOS development

Attaches the uitextfield text field control demo to the code written during the writing.

Related Article

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.