一. 修改占位字符串的 颜色:
=======方法一 ======================================
#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 = @"哈哈";
UIColor *color = [UIColor colorWithRed:100/255.0 green:200/255.0 blue:100/255.0 alpha:0.7];
[textF setValue:color forKeyPath:@"_placeholderLabel.textColor"];//修改占位字符串“哈哈”的颜色
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[_textF resignFirstResponder];
}
@end
======= 方法二 =================================
#import "MyTextField.h"
@implementation MyTextField
- (void)drawPlaceholderInRect:(CGRect)rect{
UIColor *placeholderColor = [UIColor redColor];//设置颜色
[placeholderColor setFill];
CGRect placeholderRect = CGRectMake(rect.origin.x+30, (rect.size.height- self.font.pointSize)/5, rect.size.width, self.font.pointSize);//设置距离
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineBreakMode = NSLineBreakByTruncatingMiddle;
style.alignment = self.textAlignment;
NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys: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 = @"哈哈哈哈哈哈";
// UIColor *color = [UIColor colorWithRed:100/255.0 green:200/255.0 blue:100/255.0 alpha:0.7];
// [textF setValue:color forKeyPath:@"_placeholderLabel.textColor"];//修改占位字符串“哈哈”的颜色
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[_textF resignFirstResponder];
}
@end
网友评论