Common attributes of UITextField: Delegate, redraw, and uitextviewdelegate
1. Attribute
UITextField * myTextField = [[UITextField alloc] initWithFrame: CGRectMake (50,100,200, 50)]; myTextField. backgroundColor = [UIColor clearColor]; // set the edge style of textField. borderStyle = UITextBorderStyleRoundedRect; // The content displayed by the placeholder (watermark) myTextField. placeholder = @ "Please input name"; // alignment mode myTextField. textAlignment = NSTextAlignmentLeft; // display the Clear button mode myTextField. clearButtonMode = UITextFieldViewModeWhileEditing; // set the background image // myTextField. disabledBackground // each input character changes to a vertex. used for Password Input // myTextField. secureTextEntry = YES; // clear the text when editing it again. clearsOnBeginEditing = YES; // set the keyboard style // text. keyboardType = UIKeyboardTypeNumberPad; // whether the first letter is capitalized // text. autocapitalizationType = UITextAutocapitalizationTypeNone; // What key does the return key become? // text. returnKeyType = UIReturnKeyDone; // keyboard appearance // textView. keyboardAppearance = UIKeyboardAppearanceDefault; // press the return key to access the keyboard. // becomeFirstResponder // UIView * view1 = [[UIView alloc] initWithFrame: CGRectMake (,)]; // UIImageView * imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "music.png"]; // imageView. frame = CGRectMake (0, 0, 40, 40); // UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (0, 0, 40, 40)]; // imageView. image = [UIImage imageNamed: @ "music.png"]; // left view, which can be any view under UIView. // myTextField. leftView = imageView; // display mode of the Left view // myTextField. leftViewMode = UITextFieldViewModeAlways; // right view // myTextField. rightView // display mode of the right view // myTextField. rightViewMode
2. Proxy Method
Follow the TextField proxy. myTextField. delegate = self;
To use its proxy method.
Three TextField re-painting
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. You can override the following methods. (You need to create a class that inherits from UITextField. ). -textRectForBounds: // rewrite to reset the text area-drawTextInRect: // modify the painted text attributes. during rewriting, you can call super to draw based on the default graphic attribute. If you completely rewrite the rendering function, you do not need to call super. -placeholderRectForBounds: // override to reset the placeholder area-drawPlaceholderInRect: // override to change the drawing placeholder attribute. during rewriting, you can call super to draw based on the default graphic attribute. If you completely rewrite the rendering function, you do not need to call super. -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 distortion of the button image-leftViewRectForBounds:-rightViewRectForBounds:
In actual development, the leftViewRectForBounds method is usually rewritten, And the leftView and leftViewMode attributes are set to avoid the display of input content through the top lattice.
How can I use UITextFieldDelegate to hide the keyboard?
Three steps are required:
1. Add the UITextFieldDelegate protocol to your controller class, for example:
@ Interface EditingPersonViewController: UIViewController <UITextFieldDelegate
2. Add the textFieldShouldReturn Method to the implementation file as required by the Protocol, for example:
-(BOOL) textFieldShouldReturn :( UITextField *) textField {
[TextField resignFirstResponder];
Return YES ;}
3. Direct the delegate variable of the TextField control in the xib file to the Controller class that used the UITextFieldDelegate protocol before, and right-click the delegate IBOutlet variable of the TextField to the instance of the previous controller class.
You can also use code to specify the delegate variable of the relevant TextField.
-(Void) viewDidLoad {
[Super viewDidLoad];
ItemNameField. delegate = self;
PriceField. delegate = self;} the third step is easy to ignore. Previously, because you forgot to specify the delegate variable, you can click the return key on the keyboard. The keyboard remains hidden.
Why can't UITextField treat itself as its own delegate?
If you do your own delegate, delegate is self. When an event is triggered, [delegate xxxxmethod] should wait for [self xxxxmethod]. What do you think is the problem?