Change the placeholder text and image in the text box. The text box occupies the text image.

Source: Internet
Author: User

Change the placeholder text and image in the text box. The text box occupies the text image.
To achieve this effect, click the corresponding text to highlight the placeholder text, while other text boxes do not highlight the corresponding code.

# Import <UIKit/UIKit. h> @ interface XMGTextField: UITextField/** color */@ property (nonatomic, strong) UIColor * placeholderColor; @ end # import "XMGTextField. h "# import <objc/runtime. h> static NSString * const XMGPacerholderColorKeyPath = @ "_ placeholderLabel. textColor "; @ implementation XMGTextField //-(void) drawPlaceholderInRect :( CGRect) rect {// [self. placeholder drawInRect: CGRectMake (0, 10, rect. size. width, 25) withAttributes: @ {NSForegroundColorAttributeName: [UIColor grayColor], // NSFontAttributeName: self. font //}]; //}/*** Runtime (Runtime): an official C language library of Apple can perform many low-level operations (such as accessing hidden member variables \ member methods) */-(void) awakeFromNib {// set the cursor color to be consistent with the text color self. tintColor = self. textColor; // not the first responder [self resignFirstResponder];}/*** if the current text box loses focus, it will call */-(BOOL) resignFirstResponder {// modify the placeholder text color [self setValue: [UIColor grayColor] forKeyPath: XMGPacerholderColorKeyPath]; return [super resignFirstResponder];} /*** the focus of the current text box will be called */-(BOOL) becomeFirstResponder {[self setValue: self. textColor forKeyPath: placeholder]; return [super becomeFirstResponder];}-(void) setPlaceholderColor :( UIColor *) placeholderColor {_ placeholderColor = placeholderColor; // modify the placeholder text color [self setValue: placeholderColor forKeyPath: XMGPacerholderColorKeyPath];} @ end

 

Related Article

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.