首页 > 其他 > 详细

12. UITextField

时间:2016-01-10 22:30:56      阅读:248      评论:0      收藏:0      [点我收藏+]

1. UITextField 的认识

UItextField通常用于外部数据输入,以实现人机交互。比如我们QQ、微信的登录界面中让你输入账号和密码的地方

技术分享

 

2. UITextField 控件的属性设置

#import "ViewController.h"

 

@interface ViewController ()

{

    UITextField *_textField;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blackColor];

    

    // 创建TextField

    [self creatTextField];

    

    // TextField属性设置

    [self setTextFieldPro];

    

    // TextField文本属性设置

    [self setTextOfTextFieldPro];

    

    // TextField keyBoard设置

    [self setKeyBoardOfTextField];

 

}

 

- (void)creatTextField

{

    _textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 300, 50)];

    _textField.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:_textField];

}

 

- (void)setTextFieldPro

{

    // 1. 设置边框

    //    UITextBorderStyleNone, 没有边框

    //    UITextBorderStyleLine, 线性边框

    //    UITextBorderStyleBezel, 尖角边框

    //    UITextBorderStyleRoundedRect // 圆角矩形

    _textField.borderStyle = UITextBorderStyleLine;

    

    // 2. 设置图片(设置边框不能设置UITextBorderStyleRoundedRect,否则没有效果)

    //    _textField.background = [UIImage imageNamed:@"11"];

    

    // 3. 设置编辑状态(NO,用户点击没有响应)

    _textField.enabled = YES;

}

 

- (void)setTextOfTextFieldPro

{

    // 1.默认文字

    //    _textField.text = @"奔跑吧,少年";

    

    // 2. 设置字体颜色

    _textField.textColor = [UIColor redColor];

    

    // 3. 设置文字的对齐方式

    //    _textField.textAlignment = NSTextAlignmentCenter;

        // 4. 设置文字的大小

    _textField.font = [UIFont systemFontOfSize:30];

    // 5. 设置占位文字

    //    _textField.placeholder = @"请输入密码";

        // 6. 清除原有文字

    _textField.clearsOnBeginEditing = YES;

        // 让_textField成为键盘的第一响应者

    [_textField becomeFirstResponder];

        // 判断是不是在编辑状态

    BOOL boo =  _textField.isEditing;

    // 设置清除按钮什么时候显示

    _textField.clearButtonMode = UITextFieldViewModeAlways;

   

    // 设置_textField 左视图(左右视图只能显示一个)

    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

    leftImageView.image = [UIImage imageNamed:@"TM"];

    _textField.leftView = leftImageView;

    _textField.leftViewMode = UITextFieldViewModeAlways;

    //    _textField.rightView = leftImageView;

    //    _textField.rightViewMode = UITextFieldViewModeAlways;

        // 密文显示

    //    _textField.secureTextEntry = YES;

        // 是否自动大小写

    //    UITextAutocapitalizationTypeNone,

    //    UITextAutocapitalizationTypeWords, // 单词首字母大写

    //    UITextAutocapitalizationTypeSentences,// 句子首字母大写

    //    UITextAutocapitalizationTypeAllCharacters // 全大写

    //    _textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;

    

}

 - (void)setKeyBoardOfTextField

{

    // 设置键盘的颜色

    _textField.keyboardAppearance = UIKeyboardAppearanceDark;

    // 设置键盘的类型

    // UIKeyboardTypeNumberPad 只能输入数字

    _textField.keyboardType  = UIKeyboardTypeURL;

    

    // 返回按钮的样式

    _textField.returnKeyType = UIReturnKeyNext;

    

    // 设置键盘的二级键盘

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];

    imageView.image = [UIImage imageNamed:@"TM"];

    _textField.inputAccessoryView = imageView;

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    // 移除第一响应者 (键盘退出)

    [_textField resignFirstResponder];

    NSLog(@"%@",_textField.text);

}

@end

 

3. UITextField 代理方法

@interface ViewController ()<UITextFieldDelegate>

{

    UITextField *_textField;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    [self addTextFieldToView];

    

}

 

- (void)addTextFieldToView

{

    _textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 80)];

    _textField.backgroundColor = [UIColor lightGrayColor];

    _textField.font = [UIFont systemFontOfSize:40];

    _textField.placeholder = @"请输入文字";

    

    _textField.clearButtonMode = UITextFieldViewModeAlways;

    

    // 设置代理

    _textField.delegate = self;

    [self.view addSubview:_textField];

}

 

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    NSLog(@"将要编辑");

    // YES 可以继续编辑 NO 不让编辑

    return YES;

}

 

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField

{

    NSLog(@"---%@",textField.text);

    NSLog(@"将要结束编辑的时候");

    return YES;

}

 

- (void)textFieldDidBeginEditing:(UITextField *)textField

{

    NSLog(@"已经开始编辑");

}

 

- (void)textFieldDidEndEditing:(UITextField *)textField

{

    NSLog(@"已经结束编辑");

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [_textField resignFirstResponder];

}

 

- (BOOL)textFieldShouldClear:(UITextField *)textField

{

    NSLog(@"清除的时候");

    // NO 不让清除  YES 让清除

    return NO;

}

 

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    NSLog(@"点击了Return按钮的时候");

    return YES;

}

 

#pragma mark - 用户每次输入信息的或删除的时候都调用

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    NSLog(@"%ld-- %@",range.length,string);

    return YES;

}

@end

 

4. 拓展,键盘弹出遮挡主输入框处理(处理思想是让输入框在键盘弹出的时候上移,键盘退出的时候回到原来的位置)

#import "ViewController.h"

 

@interface ViewController ()

{

    UITextField *_textField;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    // 添加UITextField

    [self addTextField];

    

    // 添加检测给键盘

    [self keyBaordShowOrHide];

}

 

- (void)addTextField

{

    CGFloat textFieldX = 20;

    CGFloat textFieldW = self.view.frame.size.width - 2 * textFieldX;

    CGFloat textFieldH = 50;

    CGFloat textFieldY = 500;

    _textField = [[UITextField alloc] initWithFrame:CGRectMake(textFieldX, textFieldY, textFieldW, textFieldH)];

    _textField.backgroundColor = [UIColor lightGrayColor];

    [self.view addSubview:_textField];

}

- (void)keyBaordShowOrHide

{

    // 检测键盘弹出

    // 1. 谁去检测

    // 2. 检测到键盘弹出执行什么方法

    // 3. 区别消息是不是键盘弹出的消息

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyBoard:) name:UIKeyboardWillShowNotification object:nil];

    

    //检测键盘消失

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyBoard:) name:UIKeyboardWillHideNotification object:nil];

}

 

- (void)hideKeyBoard:(NSNotification *)sender

{

    NSLog(@"键盘消失");// 输入框还原

  // view 位置还原

    self.view.transform = CGAffineTransformIdentity;

}

 

- (void)showKeyBoard:(NSNotification *)sender

{

    NSLog(@"键盘弹起");

    NSLog(@"%@",sender);

    // 获取键盘的高度

    CGRect rect = [[sender.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    CGFloat keyBoardH = rect.size.height;

    

    // 让整个屏幕往上移动一个键盘的高度

    self.view.transform = CGAffineTransformMakeTranslation(0, - keyBoardH + 100);

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [_textField resignFirstResponder];

}

 

@end

 

12. UITextField

原文:http://www.cnblogs.com/mcj-coding/p/5119615.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!