自定义textfeild,继承自UITextField
#import "MLCustomTextField.h"
#import "Masonry.h"
@implementation MLCustomTextField
- (instancetype)init
{
self = [super init];
if (self) {
//让光标从右边开始,有两种方法
//1.使用手势截获事件传递,然后让textFeild成为相应源(也就是忽略了光标的设置)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(resetCursorPosition)];
[self addGestureRecognizer:tap];
//2.添加一个button覆盖在textfeild上,然后截取事件响应
//跟手势不同的是,可以设置button的背景颜色,然后知道哪些没有设置
UIButton *btn = [[UIButton alloc]init];
[btn addTarget:self action:@selector(resetCursorPosition) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor redColor];
btn.alpha = 0.3;
[self addSubview:btn];
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
}];
}
return self;
}