Common attributes of UITextField: Delegate, redraw, and uitextviewdelegate

Source: Internet
Author: User

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?

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.