The IOS UI tutorial sets UITextField properties
- 2020-05-27 07:16:20
- OfStack
UITextField is a very common control in IOS, which is used to receive user input information and complete the interaction between application and user. Its main properties are set as follows:
// Initialize the textfield And set the location and size
UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];
// Set the border style. The border style will not be displayed until it is set
text.borderStyle = UITextBorderStyleRoundedRect;
typedef enum {
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
} UITextBorderStyle;
// Set the background color of the input box to white Custom background image borders are ignored if used
text.backgroundColor = [UIColor whiteColor];
// Set the background
text.background = [UIImage imageNamed:@"dd.png"];
// Set the background
text.disabledBackground = [UIImage imageNamed:@"cc.png"];
// When there is no content in the input box, the watermark is prompted The prompt content is password
text.placeholder = @"password";
// Sets the font style and size of the input box content
text.font = [UIFont fontWithName:@"Arial" size:20.0f];
// Set font color
text.textColor = [UIColor redColor];
// Is there a cross in the input box and when is it displayed for 1 Subclassically deletes the contents of the input box
text.clearButtonMode = UITextFieldViewModeAlways;
typedef enum {
UITextFieldViewModeNever, Weight does not appear
UITextFieldViewModeWhileEditing, Appear while editing
UITextFieldViewModeUnlessEditing, All except the editors
UITextFieldViewModeAlways 1 Straight a
} UITextFieldViewMode;
// In the input box 1 The text was there from the beginning
text.text = @"1 Start with the text in the input box ";
// Each input 1 The number of characters becomes a dot Password entry
text.secureTextEntry = YES;
// Whether the error correction
text.autocorrectionType = UITextAutocorrectionTypeNo;
typedef enum {
UITextAutocorrectionTypeDefault, The default
UITextAutocorrectionTypeNo, No automatic error correction
UITextAutocorrectionTypeYes, Automatic error correction
} UITextAutocorrectionType;
// Edit again and clear
text.clearsOnBeginEditing = YES;
// Content alignment
text.textAlignment = UITextAlignmentLeft;
// Vertical alignment of content UITextField Inherited from UIControl, In such a 1 A property contentVerticalAlignment
text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
// Set to YES The text will automatically shrink to fit the text window size . The default is to keep the original size , And let the long text scroll
textFied.adjustsFontSizeToFitWidth = YES;
// Set the minimum font size to automatically shrink the display
text.minimumFontSize = 20;
// Set the style of the keyboard
text.keyboardType = UIKeyboardTypeNumberPad;
typedef enum {
UIKeyboardTypeDefault, Default keyboard, all characters supported
UIKeyboardTypeASCIICapable, support ASCII Default keyboard
UIKeyboardTypeNumbersAndPunctuation, Standard phone keypad, support + * # character
UIKeyboardTypeURL, URL Keyboard, support .com button Only support URL character
UIKeyboardTypeNumberPad, The keypad
UIKeyboardTypePhonePad, The phone keyboard
UIKeyboardTypeNamePhonePad, Phone keypad, also supports entering a person's name
UIKeyboardTypeEmailAddress, For the input of electrons Email address keyboard
UIKeyboardTypeDecimalPad, The keypad There are Numbers and decimals
UIKeyboardTypeTwitter, Optimized keyboard for easy input @ , # character
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;
// Whether the first letter is capitalized
text.autocapitalizationType = UITextAutocapitalizationTypeNone;
typedef enum {
UITextAutocapitalizationTypeNone, Not automatically capitalized
UITextAutocapitalizationTypeWords, The first letter of a word is capitalized
UITextAutocapitalizationTypeSentences, The first letter of a sentence is capitalized
UITextAutocapitalizationTypeAllCharacters, All letters are capitalized
} UITextAutocapitalizationType;
//return What does the bond become
text.returnKeyType =UIReturnKeyDone;
typedef enum {
UIReturnKeyDefault, The default Grey button, labeled Return
UIReturnKeyGo, marked Go Blue button
UIReturnKeyGoogle,
marked Google The blue button, terms search
UIReturnKeyJoin,
marked Join Blue button
UIReturnKeyNext,
marked Next Blue button
UIReturnKeyRoute,
marked Route Blue button
UIReturnKeySearch,
marked Search Blue button
UIReturnKeySend,
marked Send Blue button
UIReturnKeyYahoo,
marked Yahoo Blue button
UIReturnKeyYahoo,
marked Yahoo Blue button
UIReturnKeyEmergencyCall, Emergency call button
} UIReturnKeyType;
// Keyboard appearance
textView.keyboardAppearance=UIKeyboardAppearanceDefault ;
typedef enum {
UIKeyboardAppearanceDefault . Default appearance, light grey
UIKeyboardAppearanceAlert . Dark grey Graphite color
} UIReturnKeyType;
// Set the agent Used to implement protocols
text.delegate = self;
// the textfield Add to view
[self.window addSubview:text];
// The image on the far right is the following code On the left side of the similar
UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
text.rightView=image;
text.rightViewMode = UITextFieldViewModeAlways;
typedef enum {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
} UITextFieldViewMode;
// According to the return Key the keyboard down becomeFirstResponder
Class to adopt UITextFieldDelegate agreement
text.delegate = self; The statement text The agent is me, I will implement the keyboard down the method This method is UITextFieldDelegate So we're going to use UITextFieldDelegate This agreement
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[text resignFirstResponder]; // mainly [receiver resignFirstResponder] Where can I call it receiver The corresponding keyboard is pulled down
return YES;
}
Overwrite drawing behavior
In addition to UITextField You can also customize the style options of the object UITextField Object for which a number of different override methods are added to change the display behavior of a text field. These methods all return 1 a CGRect Structure that specifies the bounds of each part of the text field. The following methods can be overridden.
� textRectForBounds: // Rewrite to reset the text area
� drawTextInRect:
// Change the emoji property . Overwrite call super You can draw by default graphic properties , If you completely override the draw function yourself, you don't need to call it super the .
� placeholderRectForBounds:
// Override to reset the placeholder area
� drawPlaceholderInRect:
// Overrides the draw placeholder property . Overwrite call super You can draw by default graphic properties , If you completely override the draw function yourself, you don't need to call it super the .
� borderRectForBounds:
// Override to reset the edge area
� editingRectForBounds:
// Override to reset the edit area
� clearButtonRectForBounds:
// Rewrite to reset clearButton location , change size May lead to button Picture distortion
� leftViewRectForBounds:
� rightViewRectForBounds:
The delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
// return 1 a BOOL Value, specifying whether to start editing in sequence from the text field
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
// When triggered to start editing, the text field will become first responder
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
// return BOOL Value that specifies whether the text field is allowed to close the edit. When the edit is finished, the text field is let go first responder
// To prevent the text field from disappearing when the user finishes editing, return NO
// the 1 Applications where text fields must always be active, such as instant messaging, are useful
return NO;
}
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
// This method is called when the user USES autocorrect to change the input text to the recommended text.
// This is especially useful for applications that want to add the undo option
// You can track what is done in the field at the end 1 You can also log all edits , For audit purposes.
// To prevent text from being changed you can return NO
// The parameters of this method are 1 a NSRange Object that indicates the location of the changed text, including the suggested modified text
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
// return 1 a BOOL The value indicates whether content is allowed to be purged upon user request
// You can set certain conditions to allow content to be cleared
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
// return 1 a BOOL Value indicating whether the edit is allowed to end when the enter key is pressed
// If allowed to call resignFirstResponder Method, which causes the edit to end and the keyboard to be retracted [textField resignFirstResponder];
// check 1 Under the resign The meaning of the word is clear
return YES;
}
The above is the entire content of this article, I hope to help you learn IOS programming.