Cat Share, must boutique
Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243
Here in code form to share to everyone, refer to Ios-textfield know how much of the blog, here Cat mainly use Markdown to optimize the directory structure, easy to read
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; 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
enum { UITextFieldViewModeNever, 重不出现 UITextFieldViewModeWhileEditing, 编辑时出现 UITextFieldViewModeUnlessEditing, 除了编辑外都出现 UITextFieldViewModeAlways 一直出现} UITextFieldViewMode;
Enter the text from the beginning of the box
text.text = @"一开始就在输入框的文字";
Every character you enter becomes a point. Password input
text.secureTextEntry = YES;
Whether to correct
enum { UITextAutocorrectionTypeDefault, 默认 UITextAutocorrectionTypeNo, 不自动纠错 UITextAutocorrectionTypeYes, 自动纠错} 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;typedefenum { UIKeyboardTypeDefault, 默认键盘,支持所有字符 UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘 UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符 UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符UIKeyboardTypeNumberPad, 数字键盘UIKeyboardTypePhonePad, 电话键盘 UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘 UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点 UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, } UIKeyboardType;
Whether the first letter is capitalized
enum { UITextAutocapitalizationTypeNone, 不自动大写 UITextAutocapitalizationTypeWords, 单词首字母大写 UITextAutocapitalizationTypeSentences, 句子的首字母大写 UITextAutocapitalizationTypeAllCharacters, 所有字母都大写} UITextAutocapitalizationType;
Return key becomes what key
enum { UIReturnKeyDefault, 默认 灰色按钮,标有Return UIReturnKeyGo, 标有Go的蓝色按钮 UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索 UIReturnKeyJoin,标有Join的蓝色按钮 UIReturnKeyNext,标有Next的蓝色按钮 UIReturnKeyRoute,标有Route的蓝色按钮 UIReturnKeySearch,标有Search的蓝色按钮 UIReturnKeySend,标有Send的蓝色按钮 UIReturnKeyYahoo,标有Yahoo的蓝色按钮 UIReturnKeyYahoo,标有Yahoo的蓝色按钮 UIReturnKeyEmergencyCall, 紧急呼叫按钮} UIReturnKeyType;
Keyboard appearance
enum {UIKeyboardAppearanceDefault, 默认外观,浅灰色UIKeyboardAppearanceAlert, 深灰 石墨色} 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 the return key to receive the keyboard down
Becomefirstresponder class to adopt 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]; //主要是[receiver resignFirstResponder]在哪调用就能把receiver对应的键盘往下收 returnYES;}
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: //重写来重置文字区域– drawTextInRect: //改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.– placeholderRectForBounds: //重写来重置占位符区域– drawPlaceholderInRect: //重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.– borderRectForBounds: //重写来重置边缘区域– editingRectForBounds: //重写来重置编辑区域– clearButtonRectForBounds: //重写来重置clearButton位置,改变size可能导致button的图片失真– 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{//Trigger on Edit, text field will become first responder} - (BOOL) Textfieldshouldendediting: (Uitextfield *) textfield{//Returns a bool value that specifies whether to allow the text field 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 in a field, or you can log all edits for auditing purposes. //To prevent text from being changed can return no//The parameter of this method has a Nsrange object, which indicates the position of the changed text, and the proposed text is also in it. return YES; } - (BOOL) Textfieldshouldclear: (Uitextfield *) textfield{//Returns a bool value indicating whether to allow content 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 indicating whether the edit is allowed to end when the ENTER key is pressed //If the Resignfirstresponder method is allowed to be called, this will result in the end of the edit and the keyboard would be closed [TextField Resignfirstresponder];//Check 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
UITextFieldTextDidBeginEditingNotificationUITextFieldTextDidChangeNotificationUITextFieldTextDidEndEditingNotification
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 //键盘显示之前发送UIKeyboardDidShowNotification //键盘显示之后发送UIKeyboardWillHideNotification //键盘隐藏之前发送UIKeyboardDidHideNotification //键盘隐藏之后发送
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 arrays by CS, array by @ "" to isolate strings BOOLCanchange = [string isequaltostring:filtered];returnCanchange;} 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 the above methodreturnBefore, do a hint, such as prompting the user can only enter numbers and the like. 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 oppositeif([String isequaltostring:@"\ n"])//Press passing to change{return YES; }NSString* Tobestring = [TextField. TextStringbyreplacingcharactersinrange: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] > -) {//If the input box content is greater than 20 pop-up warningTextField. Text= [Tobestring Substringtoindex: -]; Uialertview *alert = [[[Uialertview alloc] Initwithtitle:Nilmessage:@"More than the maximum number of words cannot be entered"DelegateNilcancelbuttontitle:@"OK"Otherbuttontitles:Nil,Nil] Autorelease]; [Alert show];return NO; } }return YES; }
Original Address http://blog.csdn.net/tskyfree/article/details/8121915/
Uitextfield full solution for cat and cat learning of iOS