参考文章:
1.写一个继承于UITextField的子类,例如:SFCustomTextField;
2.在相应的重写方法中加入你想要的操作
3.用SFCustomTextField创建相应的对象
#import "SFCustomTextField.h"
@implementation SFCustomTextField
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(CGRect)clearButtonRectForBounds:(CGRect)boundst
{ //获取clearButton的系统位置及大小 CGRect rect = [super clearButtonRectForBounds:boundst]; //将clearButton沿着X轴移动 -10个单位长度后,返回相应的rect return CGRectOffset(rect, -10, 0);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
CGRect rect = [super editingRectForBounds:bounds]; //应用于调整rect的insets UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 0 ,5);
return UIEdgeInsetsInsetRect(rect, insets);
}
@end
在其他类中使用SFCustomTextField,创建对象
#import "SFBindingOfAcountViewController.h"
@property (nonatomic, strong) SFCustomTextField *acountTextField; @property (nonatomic, strong) SFCustomTextField *passwordTextField;
@end
@implementation SFBindingOfAcountViewController
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self customUI]; }
-(void)customUI {
self.normalLoginView = [SFPublicMethod initViewOnSuperview:self.view WithFrame:CGRectMake(0, CGRectGetMaxY(line1.frame), kScreenViewWidth, 100) backgroundColor:GET_COLOR(@"#ffffff")];CGSize acountLableSize = [SFPublicMethod getTextSizeWithText:@"账户名" size:CGSizeMake(kScreenViewWidth, 49.5) font:GET_FONT(15)];UILabel *acountNameLable = [SFPublicMethod addLabelWithFrame:CGRectMake(15, 0, acountLableSize.width, 49.5) LabelFont:GET_FONT(15) LabelTextColor:GET_COLOR(@"#565c67") LabelTextAlignment:NSTextAlignmentLeft SuperView:self.normalLoginView LabelTag:773 LabelText:@"账户名"];UIView *line2 = [SFPublicMethod addLineViewFromPoint:CGPointMake(15, 50.5) Width:kScreenViewWidth - 15 SuperView:self.normalLoginView tag:884 withColor:GET_COLOR(@"#f2f2f2")];
UILabel *passwordNameLable = [SFPublicMethod addLabelWithFrame:CGRectMake(15, CGRectGetMaxY(line2.frame), acountLableSize.width, 49.5) LabelFont:GET_FONT(15) LabelTextColor:GET_COLOR(@"#565c67") LabelTextAlignment:NSTextAlignmentLeft SuperView:self.normalLoginView LabelTag:774 LabelText:@"密码"]; //输入的账号.密码
-------------使用SFCustomTextField创建对象---------------------------
self.acountTextField = [[SFCustomTextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(acountNameLable.frame) + 18, 0,kScreenViewWidth - CGRectGetMaxX(acountNameLable.frame) - 18, 49.5)];self.acountTextField.placeholder = @"用户名/邮箱/手机号";self.acountTextField.textColor = GET_COLOR(@"#000000");self.acountTextField.clearButtonMode = UITextFieldViewModeWhileEditing;self.acountTextField.delegate = self;self.acountTextField.tag = 990;[self.normalLoginView addSubview:self.acountTextField];self.acountTextField.font = GET_FONT(15);[self.acountTextField becomeFirstResponder];
---------------------使用SFCustomTextField创建对象-----------------------------
self.passwordTextField = [[SFCustomTextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(passwordNameLable.frame) + 18, CGRectGetMinY(passwordNameLable.frame),kScreenViewWidth - CGRectGetMaxX(passwordNameLable.frame) - 18, 49.5)];self.passwordTextField.placeholder = @"请输入密码";self.passwordTextField.textColor = GET_COLOR(@"#000000");self.passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;self.passwordTextField.delegate = self;self.passwordTextField.tag = 991;[self.normalLoginView addSubview:self.passwordTextField];self.passwordTextField.font = GET_FONT(15);_passwordTextField.secureTextEntry = YES;
}
@end