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. iOS推送证书转pem文件

    iOS推送证书转 .pem文件. 推送证书转pem文件openssl x509 -in apns_miaobozhibo.cer -inform der -out apns_miaobozhibo.p ...

  2. setTimeout和setInterval定时器使用详解测试

    var len=4; while(len--){ var time=setTimeout(function(){ console.log(len); },0); console.log(time); ...

  3. 日历插件FullCalendar应用:(一)数据展现

    在博客园逛了很长时间了,它帮助我获得了很多知识,很感谢大家的分享,而自己呢,由于各种纠结一直没提笔写博客,直到我看到了这篇文章http://www.cnblogs.com/zhaopei/p/why_ ...

  4. 贝赛尔曲线UIBezierPath

    使用UIBezierPath类可以创建基于矢量的路径,这个类在UIKit中.此类是Core Graphics框架关于path的一个封装.使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线 ...

  5. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  6. Jquery datatables 使用方法

    说明: 1.s开头的是字符串 2.b开头的是布尔值 3.i开头的是整型值 4.o开头的是Json对象 5.ao开头的是Json对象数组 6.aa开头的是二维数组 7.fn开头的是函数 服务器端返回的数 ...

  7. 机器学习笔记----Fuzzy c-means(FCM)模糊聚类详解及matlab实现

    前言:这几天一直都在研究模糊聚类.感觉网上的文档都没有一个详细而具体的讲解,正好今天有时间,就来聊一聊模糊聚类. 一:模糊数学 我们大家都知道计算机其实只认识两个数字0,1.我们平时写程序其实也是这样 ...

  8. js 闭包

    this.color = "blue"; (function(_this) { setInterval(function() { if (_this.color !== " ...

  9. 【原】webp图片牛刀小试

    其实今年很早就有接触到webp图片的概念,只是一直没怎么弄.今天在一个小项目中小用了一番.总结总结 采用 what,why,how的方式来总结 what? 什么是webp图片? 维基百科:       ...

  10. JavaScript------获取url地址中的参数

    $(document).ready(function () { //获取地址中的参数(name是字符串) function getParameter(name) { //正则表达式 var reg = ...