UITextField使用的一些细节

这篇博文是我自己使用UITextField的一些总结,并没有太多营养,并会持续更新。

2014.9.15

--------------------------------------------------------------------------------------------------------------------------------------

源码:

//
// RootViewController.m
// UITextField
//
// Created by YouXianMing on 14-9-15.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "RootViewController.h" @interface RootViewController ()<UITextFieldDelegate> @property (nonatomic, strong) UITextField *textField; @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; _textField = [[UITextField alloc] initWithFrame:CGRectMake(, , , )];
_textField.layer.borderWidth = .f;
_textField.layer.borderColor = [UIColor redColor].CGColor;
_textField.backgroundColor = [UIColor whiteColor];
_textField.textAlignment = kCTTextAlignmentLeft; // 左对齐
_textField.delegate = self ;
_textField.keyboardType = UIKeyboardTypeNumberPad; // 数字键盘
_textField.placeholder = @"请输入4位验证码";
_textField.clearsOnBeginEditing = YES; // 空出左侧边缘空隙(通过添加一个view来实现)
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(, , ,
CGRectGetHeight(_textField.bounds))];
_textField.leftView = view;
_textField.leftViewMode = UITextFieldViewModeAlways; [self.view addSubview:_textField]; // 添加手势
[self addGesture];
} - (void)addGesture
{
// 手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
[self.view addGestureRecognizer:tap];
} // 限制输入长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (range.location >= )
{
return NO;
}
else
{
return YES;
}
} - (void)tapGesture:(UITapGestureRecognizer *)tap
{
// 取消第一响应状态
[_textField resignFirstResponder];
} @end

效果:

左侧空出空隙

限制输入长度

--------------------------------------------------------------------------------------------------------------------------------------

最新文章

  1. 搭建Go开发及调试环境(LiteIDE + GoClipse) -- Windows篇
  2. Android JNI简介
  3. 【PowerOJ1739】 魔术球问题
  4. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
  5. Linux设置IP
  6. Log4J实用配置指南
  7. Bootstrap学习笔记之整体架构
  8. sql server split函数
  9. .offset()与.position()区别
  10. Python beautifulsoup 中文乱码
  11. Vim 神器的打造方式
  12. socket基础编程-1
  13. hadoop-1.0.1根目录下的jar包
  14. Flask系列学习
  15. Netty - 2
  16. 17-matlab例题练习
  17. 从零开始学Kotlin-数据类型(2)
  18. 生日日期联动选择birthday.js
  19. utf8 ucs4
  20. C语言中的数组的使用——混乱的内存管理

热门文章

  1. tf.data
  2. TemplateBinding和Binding的区别
  3. SharePoint 2013 EventHanlder工具
  4. ionic组件清单
  5. Linux中Redis的安装
  6. 【解决】 无法打开包括文件:“windows.h”: No such file or directory
  7. 前端框架 vue 和 react 的区别
  8. Q:链表的中间元素
  9. 重构一段基于原生JavaScript的表格绘制代码
  10. Exam E05-001 Information Storage and Management Version 3 Exam