UITextField is a very commonly used control in IOS, which is used to receive user input information and complete the interaction between applications and users. Its main properties are set as follows:
//Initialize the textfield and set the position and size UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)]; //Set the border style, the border style will be displayed only if it is set = UITextBorderStyleRoundedRect; typedef enum { UITextBorderStyleNone, UITextBorderStyleLine, UITextBorderStyleBezel, UITextBorderStyleRoundedRect } UITextBorderStyle; //Set the background color of the input box, set to white at this time. If a custom background image border is used, it will be ignored. = [UIColor whiteColor]; //Set background = [UIImage imageNamed:@""]; //Set background = [UIImage imageNamed:@""]; //When there is no content in the input box, the watermark prompts the prompt content is password = @"password"; //Set the font style and size of the input box content = [UIFont fontWithName:@"Arial" size:20.0f]; //Set font color = [UIColor redColor]; //Where is there a cross in the input box and when will it be displayed? It is used to delete the content in the input box at one time = UITextFieldViewModeAlways; typedef enum { UITextFieldViewModeNever, Reappear not UITextFieldViewModeWhileEditing, Appears when editing UITextFieldViewModeUnlessEditing, All appear except editing UITextFieldViewModeAlways Always appear } UITextFieldViewMode; //The text in the input box is available from the beginning = @"Text in the input box from the beginning"; //Every time you enter a character, it becomes a dot. = YES; //Is there any error correction = UITextAutocorrectionTypeNo; typedef enum { UITextAutocorrectionTypeDefault, default UITextAutocorrectionTypeNo, No automatic correction UITextAutocorrectionTypeYes, Automatic error correction } UITextAutocorrectionType; //Edit again and clear = YES; //Content alignment = UITextAlignmentLeft; // Vertical alignment of content UITextField inherits from UIControl, there is a property in this class contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // When set to YES, the text will automatically shrink to adapt to the text window size. The default is to keep the original size and let the long text scroll = YES; //Set the minimum font size to automatically reduce the display = 20; //Set the keyboard style = UIKeyboardTypeNumberPad; typedef enum { UIKeyboardTypeDefault, defaultkeyboard,Supports all characters UIKeyboardTypeASCIICapable, supportASCII的defaultkeyboard UIKeyboardTypeNumbersAndPunctuation, Standard phone keyboard,support+*#character UIKeyboardTypeURL, URLkeyboard,support.comButton 只supportURLcharacter UIKeyboardTypeNumberPad, 数字keyboard UIKeyboardTypePhonePad, 电话keyboard UIKeyboardTypeNamePhonePad, 电话keyboard,也support输入人名 UIKeyboardTypeEmailAddress, Used to input electronics 邮件地址的keyboard UIKeyboardTypeDecimalPad, 数字keyboard There are numbers and decimal points UIKeyboardTypeTwitter, 优化的keyboard,Convenient input@、#character UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, } UIKeyboardType; //Is the first letter capitalized = UITextAutocapitalizationTypeNone; typedef enum { UITextAutocapitalizationTypeNone, Not automatically capitalized UITextAutocapitalizationTypeWords, Capitalization of the first letter of the word UITextAutocapitalizationTypeSentences, Capital letter of sentence UITextAutocapitalizationTypeAllCharacters, All letters are capitalized } UITextAutocapitalizationType; //What key is the return key becoming =UIReturnKeyDone; typedef enum { UIReturnKeyDefault, default 灰色Button,Marked withReturn UIReturnKeyGo, Marked withGo的蓝色Button UIReturnKeyGoogle, Marked withGoogle的蓝色Button,Language Search UIReturnKeyJoin, Marked withJoin的蓝色Button UIReturnKeyNext, Marked withNext的蓝色Button UIReturnKeyRoute, Marked withRoute的蓝色Button UIReturnKeySearch, Marked withSearch的蓝色Button UIReturnKeySend, Marked withSend的蓝色Button UIReturnKeyYahoo, Marked withYahoo的蓝色Button UIReturnKeyYahoo, Marked withYahoo的蓝色Button UIReturnKeyEmergencyCall, 紧急呼叫Button } UIReturnKeyType; //Keyboard appearance=UIKeyboardAppearanceDefault; typedef enum { UIKeyboardAppearanceDefault, default外观,Light gray UIKeyboardAppearanceAlert, Dark gray Graphite color } UIReturnKeyType; //Set the proxy to implement the protocol = self; //Add textfield to view [ addSubview:text]; // Adding the picture on the far right is the following code. The left side is similar UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]]; =image; = UITextFieldViewModeAlways; typedef enum { UITextFieldViewModeNever, UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing, UITextFieldViewModeAlways } UITextFieldViewMode; //Press the return key and click the keyboard to collect it to becomeFirstResponder Classes to be adoptedUITextFieldDelegateprotocol = self; statementtextI'm the agent,我会去实现把keyboard往下收的方法 This method isUITextFieldDelegateSo we have to adoptUITextFieldDelegate这个protocol - (BOOL)textFieldShouldReturn:(UITextField *)textField { [text resignFirstResponder]; //The main thing is that the corresponding keyboard of the receiver can be collected from wherever [receiver resignFirstResponder] can be called down return YES; } Rewrite drawing behavior Apart fromUITextFieldObject style options,You can also customizeUITextFieldObject,Add many different rewrite methods to him,To change the display behavior of text fields。These methods will return aCGRectstructure,Develop boundary ranges for each component of the text field。All the following methods can be rewritten。 – textRectForBounds: //Rewrite to reset the text area– drawTextInRect: //Change the graphic attributes. When rewriting, call super can draw according to the default graphic attributes. If you completely rewrite the drawing function, you don’t need to call super.– placeholderRectForBounds: //Rewrite to reset the placeholder area– drawPlaceholderInRect: //Rewrite and change the drawing placeholder attribute. When rewrite, call super can draw according to the default graphic attributes. If you completely rewrite the drawing function, you don’t need to call super.– borderRectForBounds: //Rewrite to reset edge area– editingRectForBounds: //Rewrite to reset the editing area– clearButtonRectForBounds: //Rewrite to reset the clearButton position. Changing the size may cause the button's image to be distorted.– leftViewRectForBounds: – rightViewRectForBounds: Delegation method - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ //Return a BOOL value, specify whether to start editing in sequence text fields return YES; } - (void)textFieldDidBeginEditing:(UITextField *)textField{ //Flashed when editing begins, the text field will become the first responder} - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ //Return the BOOL value, specify whether to allow text field to end editing. When editing is finished, the text field will give up the first responder //To prevent the text field from disappearing when the user finishes editing, you can return NO //This is useful for programs where some text fields must always be active, such as instant messaging return NO; } - (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ //This method will be called when the user uses the automatic correction function to modify the input text to the recommended text.//This is especially useful for applications that want to add undo option//You can track the last modification made in the field, or log all edits for audit purposes.//To prevent the text from being changed, you can return NO//There is an NSRange object in the parameters of this method, indicating the location of the changed text, and the recommended text is also included. return YES; } - (BOOL)textFieldShouldClear:(UITextField *)textField{ //Return a BOOL value to indicate whether the content is allowed to be cleared according to user request//It can be set to allow clearing of content under specific conditions return YES; } -(BOOL)textFieldShouldReturn:(UITextField *)textField{ //Return a BOOL value indicating whether to allow the end of the edit when the Enter key is pressed //If you allow the resignFirstResponder method to be called, this time causes the editing to end, and the keyboard will be closed [textField resignFirstResponder];// Check the meaning of the word resign to understand this method return YES; }
The above is all about this article, I hope it will be helpful for everyone to learn IOS programming.