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. synergy 使用记录

    synergy 是一个多台电脑之间,共享鼠标.键盘的开源工具,做的很赞.目前,这个工具对 Windows.Linux.MacOS 都有很好的支持. 手上 arm 板安装的是 debian 系统,所以, ...

  2. Linux下几款好用的录屏软件

    最近需要在Linux环境下录制一段视频,自己的Linux是LinuxMint Xfce 18,网上搜了一圈发现都不太顺手.尤其是VLC,感觉不是很易用,幸好最后在Linux自带的软件管理器找到了两个不 ...

  3. 【总结】富有表现力的JavaScript

    1.JavaScript的灵活性 JavaScript是目前最流行.应用最广泛的语言之一,它是一种极富表现力的语言,它具有C家族语言所罕见的特性.这种语言允许我们使用各种方式来完成同一个任务或者功能, ...

  4. ECC-Elliptic Curves Cryptography,椭圆曲线密码编码学

    ECC ECC-Elliptic Curves Cryptography,椭圆曲线密码编码学,是目前已知的公钥体制中,对每比特所提供加密强度最高的一种体制.在软件注册保护方面起到很大的作用,一般的序列 ...

  5. 个人作业——week3

    1.软件工程师的成长 拜读了多位优秀前辈的文章后,感觉十分复杂,虽然说不上是醍醐灌顶的那种大彻大悟,但是确实的感觉到自己内心中某个部分被真切的感触到了.推荐的文章语言都比较平易近人,感觉就像是和多年未 ...

  6. bootstrap学习笔记--bootstrap布局方式

    Bootstrap 3 是移动设备优先的,在这个意义上,Bootstrap 代码从小屏幕设备(比如移动设备.平板电脑)开始,然后扩展到大屏幕设备(比如笔记本电脑.台式电脑)上的组件和网格. 移动设备优 ...

  7. UML大战需求分析--阅读笔记02

    这次阅读了第三章--类图.本章主要讲解了类图的基本使用规则和一些使用的例子.类图是UML中非常重要的一部分,作用很大. 类图之间有五种关系:关联关系,聚合关系,组合关系,泛化关系,依赖关系.关联关系有 ...

  8. display:none显示和隐藏

    <html> <head> <title>显示和隐藏问题</title> <meta charset="utf-8"/> ...

  9. Android之仿微信图片选择器

    先上效果图.第一张图显示的是“相机”文件夹中的所有图片:通过点击多张图片可以到第二张图所示的效果(被选择的图片会变暗,同时选择按钮变亮):点击最下面的那一栏可以到第三张图所示的效果(显示手机中所有包含 ...

  10. [Data Structure] LCSs——最长公共子序列和最长公共子串

    1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...