一、设置左、右边视图
1、设置左视图
UIImageView *leftImageView = [[UIImageView alloc] init];
leftImageView.userInteractionEnabled = userEnabled;
leftImageView.contentMode = UIViewContentModeScaleAspectFit;
leftImageView.image =[UIImage imageNamed:@"图片"];
//一直显示
self.leftViewMode = UITextFieldViewModeAlways;
self.leftView = leftImageView; //设置图像为左边视图
//右边视图同上,只需要改成这个
//如果是验证码类似功能,下面方法可以写在是否显示右边视图方法中
self.rightView = 右边视图; //且右边视图必须设置好frame
self.rightViewMode = UITextFieldViewModeAlways;
2、设置左、边视图及文字,光标、placeHolder的位置
//文字位置
- (CGRect)textRectForBounds:(CGRect)bounds{
CGRect rect = [super textRectForBounds:bounds];
return CGRectMake(左边视图的宽度+间距,
rect.origin.y,
rect.size.width - Login_leftImageWH -Login_gap,
rect.size.height);
}
- (CGRect)editingRectForBounds:(CGRect)bounds{
CGRect rect = [super editingRectForBounds:bounds];
return CGRectMake(Login_leftImageWH+Login_gap,
rect.origin.y,
rect.size.width - Login_leftImageWH -Login_gap,
rect.size.height);
}
- (CGRect)placeholderRectForBounds:(CGRect)bounds{
CGRect rect = [super placeholderRectForBounds:bounds];
return CGRectMake(Login_leftImageWH+Login_gap,
rect.origin.y,
rect.size.width - Login_leftImageWH -Login_gap,
rect.size.height);
}
- (CGRect)leftViewRectForBounds:(CGRect)bounds{
CGRect rect = [super leftViewRectForBounds:bounds];
return CGRectMake(2,
rect.origin.y,
Login_leftImageWH,
Login_leftImageWH);
}
- (CGRect)rightViewRectForBounds:(CGRect)bounds{
CGRect rect = [super rightViewRectForBounds:bounds];
return CGRectMake(rect.origin.x,
rect.origin.y,
Login_rightViewWidth,
rect.size.height);
}
二、光标的设置
1、 设置光标的颜色
1、如何是xib或storyBoard在设置控件属性找有没有有关颜色的属性(找到了backgroundColor,TextColor,还有一个是tintColor),这样就排除了前两个,试下tintColor,果然有效。
2、如何是用代码写的,可以在UITextField中找相关color,如果没有我们要的属性在父类里找或父类的父类中找,这时找到了这个tintColor。
2、设置点位符的颜色
方法1:
1.因为上面已经找过了没有其它跟颜色相关的属性了,这时我们在UITextField中找占位符相关的属性placeHolder,找到了下面4个
@property(nullable, nonatomic,copy) NSString *placeholder; //这个是设置文字的排除
@property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder //这个可能是
//这个显示不是,返回是rect和颜色无关
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
//这个是重新画placeholder占位文字的,可能是
- (void)drawPlaceholderInRect:(CGRect)rect;
2. 先用这个attributedPlaceholder,因为它是个NSAttributedString,所以可以通过创建来设置颜色相关及字体相关的东西。
3、最后结果:
//设置占位符的颜色
NSMutableDictionary *attDic = [NSMutableDictionary dictionary];
attDic[NSForegroundColorAttributeName] = [UIColor whiteColor];
//这里把当前的占位文字传过来,再设置attributes属性
NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:self.placeholder attributes:attDic];
self.attributedPlaceholder = attribute;
方法2:
//1.通过下面的方法来实现占位文字的改变
- (void)drawPlaceholderInRect:(CGRect)rect;
//2.具体实现代码如下:
//第二种设置占位符的颜色
- (void)drawPlaceholderInRect:(CGRect)rect{
CGRect rects = rect; //因为这里的rect就是占位文字的rect,所以只需要设置它的origin就可以
rects.origin.x = 0;
rects.origin.y = (rect.size.height - self.font.lineHeight)/2;
NSMutableDictionary *attDic = [NSMutableDictionary dictionary];
attDic[NSForegroundColorAttributeName] = [UIColor whiteColor];
[self.placeholder drawInRect:rects withAttributes:attDic];
}
方法3:
1、但凡是设置这种属性的颜色及其它的都可以通过KVC来实现。
2、通过查看占位这个框里面是个文本Label。
3、然后在xib中打印下它的子类,因为xib加载比较慢,所以加个延时
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//打印了一下发现它是的textFieldLabel类,父类是UILabel,所以这个可以取出来设置颜色即可,
//但有一点不好的是,它需要延时加载,感觉有点low
NSLog(@"%@%@",self.subviews,self.subviews.lastObject.superclass);
//设置它的颜色,
UILabel *label = self.subviews.lastObject;
label.textColor = [UIColor whiteColor];
});
4、通过runtime运行时机制,遍历一下UITextField这个类所有属性,看看它有没有placeholder相关的label
#import <objc/runtime.h>
// 通过运行时来遍历每个属性
unsigned int count;
//class_copyIvarList 获取所有属性数组
Ivar *varList = class_copyIvarList([UITextField class],&count);
for (NSInteger i = 0; i < count; i++) {
Ivar var = varList[i];
//遍历所有属性的名字
NSLog(@"%s",ivar_getName(var));
}
//因为是c语言,所以这里需要释放
free(varList);
通过上面的运行时可以打印出一个名叫placeholderLabel,这个正是我们要的
5、通过kvc的方式来设置,这里用的是forKeyPath,不是forkey,因为从forkey中找不到
[self setValue:[UIColor whiteColor] forKeyPath:placeholdKey];
6、实现在编辑时白色,不编辑时白色,通过addTarget添加两个监听,开始编辑时和结束编辑时
static NSString * const placeholdKey = @"placeholderLabel.textColor"; //添加个静态公共属性,方便多个地方使用
[self addTarget:self action:@selector(beginEdit) forControlEvents:UIControlEventEditingDidBegin];
[self addTarget:self action:@selector(endEdit) forControlEvents:UIControlEventEditingDidEnd];
- (void)beginEdit{
[self setValue:[UIColor whiteColor] forKeyPath:placeholdKey];
}
- (void)endEdit{
[self setValue:[UIColor grayColor] forKeyPath:placeholdKey];
}