菜鸟启航:UITextField基本操作(二)

本文介绍了如何通过继承UITextField来自定义其绘制和位置属性,并详细解释了UITextFieldDelegate协议的方法及其应用场景,包括键盘显示隐藏的通知处理。

一、继承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;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值