美文网首页
UITextField监听键盘输入,设置leftView大小

UITextField监听键盘输入,设置leftView大小

作者: _RG | 来源:发表于2019-12-18 12:00 被阅读0次

配合ReactiveObjC框架中提供的rac_textSignal的API就可以全面的监听按钮输入

 [_searchTF.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
  
}];

注意

通过UITextFieldDelegate方法- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 来监听输入时会不准确, 此方法监听到的text会少最后一个输入的字符或文字, 一般此方法用来做控制字数限制的操作

设置leftView的大小

生成一个继承UITextField的子类, 重写leftViewRectForBounds方法


@interface CRLTextField : UITextField

@end

@implementation CRLTextField

- (CGRect)leftViewRectForBounds:(CGRect)bounds {
    
    return CGRectMake(0, 0, 32, 32);
}

- (CGRect)rightViewRectForBounds:(CGRect)bounds {
    
    return CGRectMake(bounds.size.width-32, 0, 32, 32);
}

@end

相关文章

网友评论

      本文标题:UITextField监听键盘输入,设置leftView大小

      本文链接:https://www.haomeiwen.com/subject/idaynctx.html