Ios-textfield Control Detailed

Source: Internet
Author: User
Tags set background uicontrol

Initialize TextField and set location and size

Uitextfield *text = [[Uitextfield alloc]initwithframe:cgrectmake (20, 20, 130, 30)];

Set border style to show border style only if set

Text.borderstyle = Uitextborderstyleroundedrect;

typedef enum {

Uitextborderstylenone,

Uitextborderstyleline,

Uitextborderstylebezel,

Uitextborderstyleroundedrect

} Uitextborderstyle;

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];

Set background

Text.background = [UIImage imagenamed:@ "Dd.png"];

Set background

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

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

Text.placeholder = @ "password";

Set font style and size for input box contents

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

Set Font Color

Text.textcolor = [Uicolor Redcolor];

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;

Enter the text from the beginning of the box

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

Every character you enter becomes a point. Password input

Text.securetextentry = YES;

Whether to correct

Text.autocorrectiontype = Uitextautocorrectiontypeno;

typedef enum {

Uitextautocorrectiontypedefault, default

Uitextautocorrectiontypeno, no automatic error correction

Uitextautocorrectiontypeyes, automatic error correction

} Uitextautocorrectiontype;

Edit it again and empty it.

text.clearsonbeginediting = YES;

Content Alignment

Text.textalignment = Uitextalignmentleft;

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

Text.contentverticalalignment = Uicontrolcontentverticalalignmentcenter;

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;

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;

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;

Return key becomes what key

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;

Keyboard appearance

Textview.keyboardappearance=uikeyboardappearancedefault;

typedef enum {

Uikeyboardappearancedefault, default appearance, light grey

Uikeyboardappearancealert, dark grey graphite

} Uireturnkeytype;

Setting up proxies for implementing protocols

Text.delegate = self;

Add the TextField to the view

[Self.window Addsubview:text];

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;

Press RETURN key to receive Becomefirstresponder

class to adopt the Uitextfielddelegate protocol

Text.delegate = self; Declare the text of the agent is me, I will go to the way to get the keyboard down the method in uitextfielddelegate so we have to use uitextfielddelegate this protocol

-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField

{

[Text Resignfirstresponder]; The main is [receiver Resignfirstresponder] where the call to the receiver corresponding to the keyboard down

return YES;

}

overriding Paint behavior

In addition to the style options of the Uitextfield object, you can also customize the Uitextfield object and add many different rewrite methods to it to change the display behavior of the text field. All of these methods return a CGRECT structure that has a boundary range for each part of the text field. The following methods can be overridden.

–textrectforbounds://rewrite to reset text area

–drawtextinrect://Change the Emoji property. When overridden, calling super can be drawn by default drawing properties, so you don't have to call super if you completely override the drawing function.

–placeholderrectforbounds://Override to reset placeholder area

–drawplaceholderinrect://Override to change the draw placeholder property. When overridden, calling super can be drawn by default drawing properties, so you don't have to call super if you completely override the drawing function.

–borderrectforbounds://Rewrite to reset the edge area

–editingrectforbounds://Rewrite to reset the editing area

–clearbuttonrectforbounds://Rewrite to reset the Clearbutton position, changing the size may cause the picture of the button to be distorted

–leftviewrectforbounds:

–rightviewrectforbounds:

Delegate method

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

Returns a bool value that specifies whether the sequential text field begins editing

return YES;

}

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

Triggered when editing is started, the text field becomes first responder

}

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

Returns a bool value that specifies whether the text field is allowed to end editing, and when the edit is finished, the text field will yield first responder

To prevent the text field from disappearing when the user finishes editing, you can return no

This is useful for programs where text fields must always remain active, such as instant messaging

return NO;

}

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

This method is called when the user uses the AutoCorrect feature to modify the input text to the recommended text.

This is especially useful for applications that want to join the Undo option

You can track the last modification made within a field, or you can log all edits for auditing purposes.

To prevent text from being changed, you can return no

There is a Nsrange object in the parameter of this method, which indicates the position of the changed text, and the suggested text is in it.

return YES;

}

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

Returns a bool value indicating whether content is allowed to be purged based on user request

Can be set under certain conditions to allow purging of content

return YES;

}

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

Returns a bool value that indicates whether the edit is allowed to end when the ENTER key is pressed

If you allow the Resignfirstresponder method to be called, this causes the end of the edit, and the keyboard will be closed [TextField Resignfirstresponder];

Check out the meaning of the word resign to understand this method.

return YES;

}

Notice

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

Uitextfieldtextdidbegineditingnotification

Uitextfieldtextdidchangenotification

Uitextfieldtextdidendeditingnotification

triggered when the text field exits edit mode. The object property of the notification stores the final text.

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

Uikeyboardwillshownotification//keyboard display before sending

Uikeyboarddidshownotification//keyboard display after send

Uikeyboardwillhidenotification//keyboard hidden before sending

Uikeyboarddidhidenotification//keyboard hidden after sending

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.

19. Secure: When your text box is used as a password input box, you can select this option, at which point the characters are displayed as asterisks.

1.Alignment Horizontal Horizontal alignment

2.Alignment Vertical Vertical Alignment

3. Returns a bool value if the input box is Selected (checked) Enabled (available) highlighted (highlighted)

Restrict input of only specific characters

(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) Range replacementstring: (nsstring *) string{

Nscharacterset *cs;

CS = [[Nscharacterset charactersetwithcharactersinstring:numbers]invertedset];

NSString *filtered = [[string componentsseparatedbycharactersinset:cs]componentsjoinedbystring:@ "]; Separate groups by CS, array separated by @ ""

BOOL canchange = [string isequaltostring:filtered];

return canchange;

}

The numbers above is a macro that can be defined at the top of the file:

#define NUMBERS @ "0123456789\n" (this representative can enter numbers and line breaks, please note this \ n, if you do not write this, the Done button will not be triggered, if used in Searchbar, will not trigger the search event, Because you restrict the input \ n, I found it in the project. )

So, if you want to restrict the input of English and numbers, you can define this as:

#define Kalphanum @ "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789″.

Of course, you can also make a hint before the above method return, such as prompting the user to enter only numbers. If you feel the need.

Restrict the input of only a certain length of characters

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

{//string is the character entered at this time TextField is the input box that is being entered returns Yes to change the value of the input box no opposite

if ([string isequaltostring:@ "\ n"])//press passing to change

{

return YES;

}

NSString * tobestring = [Textfield.text stringbyreplacingcharactersinrange:range withstring:string]; Get the contents of the input box

if (Self.mytextfield = = TextField)//Determine if we want to limit the input box

{

if ([tobestring length] > 20) {//Popup warning if input box content is greater than 20

Textfield.text = [tobestring substringtoindex:20];

Uialertview *alert = [[[Uialertview alloc] Initwithtitle:nil message:@ "The maximum number of words cannot be entered" Delegate:nil cancelbuttontitle:@ " OK "otherbuttontitles:nil, nil] autorelease];

[Alert show];

return NO;

}

}

return YES;

}

Ios-textfield Control Detailed

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.