一、继承UITextField的类
// drawing and positioning overrides
- (CGRect)borderRectForBounds:(CGRect)bounds;
- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
- (CGRect)editingRectForBounds:(CGRect)bounds;
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
- (CGRect)leftViewRectForBounds:(CGRect)bounds;
- (CGRect)rightViewRectForBounds:(CGRect)bounds;
- (void)drawTextInRect:(CGRect)rect;
- (void)drawPlaceholderInRect:(CGRect)rect;
////边框的坐标和大小
//- (CGRect)borderRectForBounds:(CGRect)bounds
//{
// //边框的大小
// //缩减
// NSLog(@"before: %@",NSStringFromCGRect(bounds));
// CGRect rect = CGRectInset(bounds, 50, 10);
// NSLog(@"after: %@",NSStringFromCGRect(rect));
// return rect;
//}
//
////非编辑状态下,文本的显示区域
//- (CGRect)textRectForBounds:(CGRect)bounds
//{
// return CGRectInset(bounds, 50, 10);
//}
//
////占位符的显示区域
//- (CGRect)placeholderRectForBounds:(CGRect)bounds
//{
// return CGRectMake(50, 10, 200, 40);
//}
//
////编辑状态下,文本的显示区域
//- (CGRect)editingRectForBounds:(CGRect)bounds
//{
// return CGRectInset(bounds, 50, 10);
//}
//
//- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
//{
// return CGRectOffset(bounds, -50, 0);
//}
//- (CGRect)leftViewRectForBounds:(CGRect)bounds;
//- (CGRect)rightViewRectForBounds:(CGRect)bounds;
//
- (void)drawTextInRect:(CGRect)rect
{
[super drawTextInRect:CGRectInset(rect, 50, 10)];
}
- (void)drawPlaceholderInRect:(CGRect)rect
{
[super drawPlaceholderInRect:CGRectInset(rect, 50, 10)];
}
二、通知中心
//通知中心
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShowNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHideNotification:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyBoardShowNotification:(NSNotification *)notificaiton
{
NSLog(@"键盘显示");
[UIView animateWithDuration:1.0 animations:^{
self.button.frame = CGRectOffset(self.button.frame, 0, -100);
}];
}
- (void)keyBoardHideNotification:(NSNotification *)notificaiton
{
NSLog(@"键盘隐藏");
[UIView animateWithDuration:1.0 animations:^{
self.button.frame = CGRectOffset(self.button.frame, 0, 100);
}];
}
三、协议UITextFieldDelegate
self.tf.delegate = self;
协议可选实现方法
@protocol UITextFieldDelegate <NSObject>
@optional
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
@end
//询问delegate我是否可以进行编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"Delegate 被告知,TextField已经处于编辑状态,请指示");
}
//询问,TextField是否可以结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(@"询问,TextField是否可以结束编辑");
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"TextField被告知,已经结束编辑状态");
}
//询问,是否可以改变指定TextField的指定的range范围内的内容替换成指定的string
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// if ([string isEqualToString:@"a"]) {
// return YES;
// }
NSString * originString = textField.text;
NSString * newString = [originString stringByReplacingCharactersInRange:range withString:string];
if (newString.length < 10) {
return YES;
}
return NO;
}
//
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
NSLog(@"询问是否可以清除内容");
return NO;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"Return键被点击");
if (textField == self.tf) {
[self.pwtf becomeFirstResponder];
}
else {
[textField resignFirstResponder];
}
return YES;
}