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的更多相关文章

  1. python 各模块

    01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...

  2. Python Standard Library

    Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...

  3. 在mybatis中写sql语句的一些体会

    本文会使用一个案例,就mybatis的一些基础语法进行讲解.案例中使用到的数据库表和对象如下: article表:这个表存放的是文章的基础信息 -- ------------------------- ...

  4. iOS开发笔记12:iOS7上UITextField限制字数输入导致崩溃问题

    在一些场景中,需要限制用户的输入字数,例如在textField里进行控制(textView也类似,崩溃原因也相同),如图所示 系统会监听文本输入,需要注意的第一点是输入法处于联想输入还未确定提交的时候 ...

  5. UITextField使用详解

    转iOS中UITextField使用详解 (1) //初始化textfield并设置位置及大小   UITextField *text = [[UITextField alloc]initWithFr ...

  6. iOS阶段学习第29天笔记(UITextField的介绍)

    iOS学习(UI)知识点整理 一.关于UITextField的介绍 1)概念: UITextField 是用于接收用户输入的一个控件 2)UITextField  初始化实例代码: //创建一个UIt ...

  7. 你真的了解UITextField吗?

    一:首先查看一下关于UITextField的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextField : UIControl <UITextIn ...

  8. UI第三节—— UITextField详解

    戏言:UITextField对于需要登陆注册的界面的作用还是相当明显,但是对于键盘过的遮挡问题,可是重点哦!这里就涉及到通知(NSNotificationCenter)的内容. //注册事件 [[NS ...

  9. Swift - 文本输入框(UITextField)

    1,文本框的创建,有如下几个样式: UITextBorderStyle.none:无边框 UITextBorderStyle.line:直线边框 UITextBorderStyle.roundedRe ...

随机推荐

  1. js定位

    1.引入 百度地图js(1.3以后需要key) <script type="text/javascript" src="http://api.map.baidu.c ...

  2. JS命名空间

    命名冲突 全局变量会绑定到 window 上,不同的javascript文件如果使用了相同的全局变量,或者定义了相同名字就的顶层函数,都会造成命名冲突,并且很难被发现. 减少冲突的一个办法,把自己的所 ...

  3. java 方法

    方法命名规范要求 类的命名规范:“全部单词的 首字母必须大写”.那么在定义方法的时候也是有命名规范要求的:“第 一个单词的首字母小写,之后每个单词的首字母大写”,那么这就是方法 的命名规范. 递归调用 ...

  4. Chrome 开发工具之Elements

    友情提示:全文图片高能,如使用手机阅读,请确保在wifi情况下或者流量充足.图片有点渣,也算辛苦做出来的,请别嫌弃- Elements面板主要展示当前页面的组织结构,在如今的应用程序中,HTML页面初 ...

  5. Web系统大规模并发——电商秒杀与抢购

    电商的秒杀和抢购,对我们来说,都不是一个陌生的东西.然而,从技术的角度来说,这对于Web系统是一个巨大的考验.当一个Web系统,在一秒钟内收到数以万计甚至更多请求时,系统的优化和稳定至关重要.这次我们 ...

  6. 14 Iterator和for...of循环

    Iterator和for...of循环 首先 Iterator 是一个接口. 标准是 function makeIterator(array) { var nextIndex = 0; return ...

  7. 移动端阻止body滚动

    一些移动设备有缺省的touchmove行为,比如说经典的iOS overscroll效果,当滚动超出了内容的界限时就引发视图反弹 阻止滚动: css: body{ height:100%; overf ...

  8. UnicodeEncodeError: 'ascii' codec can't encode characters in position 820-823: ordinal not in range(128)

    真是奇怪了,在itermi里 print(data) 就能直接运行,而在sublime里,就非得写成这样 print(data.encode('utf-8'))

  9. 关于JSP中<body onload="fun()">body标签中onload中函数不执行问题

    问题描述: 在一个页面中,我们经常会初始化一下数据,而且会在指定的DOM元素初始化数据,这时候我们就会使用<body onload="fun()">来加载我们的数据.o ...

  10. Ubuntu 手动更新firefox的flash插件

    Ubuntu下 Firefox更新flash插件老是提示失败,自己动手丰衣足食啊. 1.下载tar文件,地址:http://get.adobe.com/cn/flashplayer/?no_redir ...