UITextFeild usage and UITextFeild usage
1. Modify the color of the placeholder string:
= ========
# Import "ViewController. h"
# Import "MyTextField. h"
@ Interface ViewController ()
{
UITextField * _ textF;
}
@ End
@ Implementation ViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
Self. view. backgroundColor = [UIColor whiteColor];
MyTextField * textF = [[MyTextField alloc] initWithFrame: CGRectMake (100,100,200, 40)];
_ TextF = textF;
[Self. view addSubview: textF];
TextF. backgroundColor = [UIColor whiteColor];
TextF. borderStyle = UITextBorderStyleRoundedRect;
TextF. leftViewMode = UITextFieldViewModeWhileEditing;
UIButton * btn = [[UIButton alloc] initWithFrame: CGRectMake (0, 0, 30, 30)];
Btn. backgroundColor = [UIColor greenColor];
TextF. leftView = btn;
TextF. placeholder = @ "Haha ";
UIColor * color = [UIColor colorWithRed: 100/255. 0 green: 200/255. 0 blue: 100/255. 0 alpha: 0.7];
[TextF setValue: color forKeyPath: @ "_ placeholderLabel. textColor"]; // modify the color of the placeholder string "Haha"
}
-(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event
{
[_ TextF resignFirstResponder];
}
@ End
Running effect:
1. The editing status is not displayed (textF. leftView = btn; hide ):
2. Enter the editing status (textF. leftView = btn; displayed ):
========= Method 2 ========================================= =
# Import "MyTextField. h"
@ Implementation MyTextField
-(Void) drawPlaceholderInRect :( CGRect) rect {
UIColor * placeholderColor = [UIColor redColor]; // set the color.
[PlaceholderColor setFill];
CGRect placeholderRect = CGRectMake (rect. origin. x + 30, (rect. size. height-self. font. pointSize)/5, rect. size. width, self. font. pointSize); // set the distance
NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
Style. lineBreakMode = NSLineBreakByTruncatingMiddle;
Style. alignment = self. textAlignment;
NSDictionary * attr = [NSDictionary attributes: style, NSParagraphStyleAttributeName, self. font, NSFontAttributeName, placeholderColor, NSForegroundColorAttributeName, nil];
[Self. placeholder drawInRect: placeholderRect withAttributes: attr];
}
@ End
# Import "ViewController. h"
# Import "MyTextField. h"
@ Interface ViewController ()
{
UITextField * _ textF;
}
@ End
@ Implementation ViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
Self. view. backgroundColor = [UIColor whiteColor];
MyTextField * textF = [[MyTextField alloc] initWithFrame: CGRectMake (100,100,200, 40)];
_ TextF = textF;
[Self. view addSubview: textF];
TextF. backgroundColor = [UIColor whiteColor];
TextF. borderStyle = UITextBorderStyleRoundedRect;
TextF. leftViewMode = UITextFieldViewModeWhileEditing;
UIButton * btn = [[UIButton alloc] initWithFrame: CGRectMake (0, 0, 30, 30)];
Btn. backgroundColor = [UIColor greenColor];
TextF. leftView = btn;
TextF. placeholder = @ "Haha ";
// UIColor * color = [UIColor colorWithRed: 100/255. 0 green: 200/255. 0 blue: 100/255. 0 alpha: 0.7];
// [TextF setValue: color forKeyPath: @ "_ placeholderLabel. textColor"]; // modify the color of the placeholder string "Haha"
}
-(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event
{
[_ TextF resignFirstResponder];
}
@ End
Running effect: