菜鸟启航:UITextField基本操作(二)
2015-09-07 12:54
489 查看
一、继承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;
}
// 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;
}
相关文章推荐
- 菜鸟启航:UITextField基本操作(一)
- UITable排序问题
- 菜鸟启航:UIButton基本操作
- sql2005 OPENDATASOURCE 需要Ad Hoc Distributed Quer
- LeetCode || N-Queens
- UITabBar 【标签条】自己的见解
- (不连续)最大公共子序列POJ 1458Common Subsequence
- UItableView 使用心得
- 【游戏科普】使用心理学法则创建优秀的用户界面
- quick-3.5 无法正常显示有混合纹理的csb文件
- UNIGUI:How to redirect and close session?
- UICollectionView基础
- MUI - sortable在mui.js前端框架不兼容的解决方案
- BlockingQueue深入分析
- VS2010 MFC的小Bug-ASSERT(ContinueModal());
- SOAPUI测试步骤(四)---The Script TestStep
- SOAPUI测试步骤(三)---Run TestCase
- SOAPUI测试步骤(二)---流量控制(Conditional Goto)
- SOAPUI测试步骤(一)——断言测试(Assertion TestStep)
- SOAPUI----安全扫描(Security Testing)