每次设置这种textField的左右视图都是一堆相似的代码,已经忍了很久了,方案可以用category也可以自定义textField去抽,想了一下为了少侵入同事的代码还是采用category直接设置UITextField了。
采用了链式调用设置文本框 ,unit是UIView或者其子类的实例,unitSize是rightView的尺寸,insets的left和right属性可以控制rightView距离文字和文本框的距离
以下就是设置一个rightView的调用方法,简洁了很多。
textField.setRightView(unit).setRightViewSize(unitSize).setRightViewInsets(UIEdgeInsetsMake(0, 0, 0, 5)).config();
未完善的问题
设置了rightView后就不能显示clearButton了,在category中重写了以下方法也无济于事,求指导。
- (CGRect)borderRectForBounds:(CGRect)bounds;
- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)editingRectForBounds:(CGRect)bounds;
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
category代码
UITextField+ZJExtension.h
#import
@interface UITextField (ZJExtension)
- (UITextField *(^)(UIView *))setLeftView;
- (UITextField *(^)(CGSize))setLeftViewSize;
- (UITextField *(^)(UIEdgeInsets))setLeftViewInsets;
- (UITextField *(^)(UIView *))setRightView;
- (UITextField *(^)(CGSize))setRightViewSize;
- (UITextField *(^)(UIEdgeInsets))setRightViewInsets;
- (void (^)())config;
@end
UITextField+ZJExtension.m
#import "UITextField+ZJExtension.h"
#import
static CGFloat clearButtonWH = 16.0f;
@interface UITextField ()
@property(nonatomic,strong)UIView *left_view;
@property(nonatomic,assign)CGSize leftView_size;
@property(nonatomic,assign)UIEdgeInsets leftView_insets;
@property(nonatomic,strong)UIView *right_view;
@property(nonatomic,assign)CGSize rightView_size;
@property(nonatomic,assign)UIEdgeInsets rightView_insets;
@end
@implementation UITextField (ZJExtension)
#pragma mark - leftViewConfig
- (UITextField * (^)(UIView *))setLeftView
{
return ^(UIView *leftView){
self.left_view = leftView;
return self;
};
}
- (void)setLeft_view:(UIView *)left_view
{
objc_setAssociatedObject(self, @selector(left_view), left_view, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)left_view
{
return objc_getAssociatedObject(self, _cmd);
}
- (UITextField * (^)(CGSize))setLeftViewSize
{
return ^(CGSize size){
self.leftView_size = size;
return self;
};
}
- (void)setLeftView_size:(CGSize)leftView_size
{
objc_setAssociatedObject(self, @selector(leftView_size), [NSValue valueWithCGSize:leftView_size], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGSize)leftView_Size
{
NSValue *value = objc_getAssociatedObject(self, _cmd);
CGSize size = value.CGSizeValue;
return size;
}
- (UITextField * (^)(UIEdgeInsets))setLeftViewInsets
{
return ^(UIEdgeInsets insets){
self.leftView_insets = insets;
return self;
};
}
- (void)setLeftView_insets:(UIEdgeInsets)leftView_insets
{
objc_setAssociatedObject(self, @selector(leftView_insets), [NSValue valueWithUIEdgeInsets:leftView_insets], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIEdgeInsets)leftView_insets
{
NSValue *value = objc_getAssociatedObject(self, _cmd);
UIEdgeInsets insets = value.UIEdgeInsetsValue;
return insets;
}
#pragma mark - rightViewConfig
- (UITextField * (^)(UIView *))setRightView
{
return ^(UIView *leftView){
self.right_view = leftView;
return self;
};
}
- (void)setRight_view:(UIView *)right_view
{
objc_setAssociatedObject(self, @selector(right_view), right_view, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)right_view
{
return objc_getAssociatedObject(self, _cmd);
}
- (UITextField * (^)(CGSize))setRightViewSize
{
return ^(CGSize size){
self.rightView_size = size;
return self;
};
}
- (void)setRightView_size:(CGSize)rightView_size
{
objc_setAssociatedObject(self, @selector(rightView_size), [NSValue valueWithCGSize:rightView_size], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGSize)rightView_size
{
NSValue *value = objc_getAssociatedObject(self, _cmd);
CGSize size = value.CGSizeValue;
return size;
}
- (UITextField * (^)(UIEdgeInsets))setRightViewInsets
{
return ^(UIEdgeInsets insets){
self.rightView_insets = insets;
return self;
};
}
- (void)setRightView_insets:(UIEdgeInsets)rightView_insets
{
objc_setAssociatedObject(self, @selector(rightView_insets), [NSValue valueWithUIEdgeInsets:rightView_insets], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIEdgeInsets)rightView_insets
{
NSValue *value = objc_getAssociatedObject(self, _cmd);
UIEdgeInsets insets = value.UIEdgeInsetsValue;
return insets;
}
- (void(^)())config
{
return ^{
self.returnKeyType = UIReturnKeyDone;
self.clearButtonMode = UITextFieldViewModeWhileEditing;
CGFloat textFieldH = self.bounds.size.width;
if (self.left_view) {
UIView *leftBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.leftView_size.width+self.leftView_insets.left+self.leftView_insets.right, textFieldH)];
self.left_view.frame = CGRectMake(self.leftView_insets.left, (textFieldH-self.leftView_size.height)*0.5, self.leftView_size.width, self.leftView_size.height);
[leftBackView addSubview:self.left_view];
self.leftView = leftBackView;
self.leftViewMode = UITextFieldViewModeAlways;
}
if (self.right_view) {
UIView *rightBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.rightView_size.width+self.rightView_insets.left+self.rightView_insets.right, textFieldH)];
self.right_view.frame = CGRectMake(self.rightView_insets.left, (textFieldH-self.rightView_size.height)*0.5, self.rightView_size.width, self.rightView_size.height);
[rightBackView addSubview:self.right_view];
self.rightView = rightBackView;
self.rightViewMode = UITextFieldViewModeAlways;
}
};
}
///** 覆盖系统方法 */
//- (CGRect)clearButtonRectForBounds:(CGRect)bounds
//{
// CGRect rect = CGRectMake(bounds.size.width-CGRectGetWidth(self.rightView.frame)-clearButtonWH, (bounds.size.height-clearButtonWH)*0.5, clearButtonWH, clearButtonWH);
// return rect;
//}
//
////控制编辑文本的位置
//- (CGRect)editingRectForBounds:(CGRect)bounds
//{
// //return CGRectInset( bounds, 10 , 0 );
// CGFloat editingTextX = CGRectGetMaxX(self.leftView.frame);
// CGRect inset = CGRectMake(editingTextX, bounds.origin.y, bounds.size.width-editingTextX-self.rightView.bounds.size.width-clearButtonWH, bounds.size.height);
// return inset;
//}
@end