UI2_UITextField
//
// ViewController.h
// UI2_UITextField
//
// Created by zhangxueming on 15/7/2.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITextFieldDelegate> //遵守协议 @end //
// ViewController.m
// UI2_UITextField
//
// Created by zhangxueming on 15/7/2.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController
//UITextField --- 文本框 - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, self.view.frame.size.width-40, 50)];
textField.backgroundColor = [UIColor cyanColor];
//设置文本框的风格
//设置圆角型风格
textField.borderStyle = UITextBorderStyleRoundedRect;
//设置默认提示文本,当开始编辑内容时候, 提示内容消失
textField.placeholder = @"请输入文字";
//设置文本框初始内容
textField.text = @"hello world";
//设置文本内容颜色
textField.textColor = [UIColor redColor];
//设置文本内容字体
textField.font = [UIFont boldSystemFontOfSize:24];
//设置文本对齐方式
//textField.textAlignment = NSTextAlignmentCenter;
//设置编辑文本时,清空原文本内容
textField.clearsOnBeginEditing = YES;
//设置显示清除按钮
//UITextFieldViewModeNever,
//UITextFieldViewModeWhileEditing,
//UITextFieldViewModeUnlessEditing,
//UITextFieldViewModeAlways
textField.clearButtonMode = UITextFieldViewModeAlways;
//设置左视图
UIImageView *leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"003.png"]];
//设置左视图的origin位置无效
leftImageView.frame = CGRectMake(0, 0, 30, 30);
textField.leftViewMode = UITextFieldViewModeAlways;
textField.leftView = leftImageView;
//设置右视图
UIImageView *rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];
rightImageView.frame = CGRectMake(0, 0, 30, 30);
textField.rightViewMode = UITextFieldViewModeAlways;
textField.rightView = rightImageView;
//在模拟器上切换键盘 command + K
//shift + command + k 开启电脑键盘 //设置暗文输入
textField.secureTextEntry = YES;
//设置键盘风格
//UIKeyboardTypeDefault, 默认键盘,支持所有字符
//UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘
//UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符
//UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符
//UIKeyboardTypeNumberPad, 数字键盘
//UIKeyboardTypePhonePad, 电话键盘
//UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名
//UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘
//UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点
//UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符
//UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, textField.keyboardType = UIKeyboardTypeURL;
//设置键盘return键风格
textField.returnKeyType = UIReturnKeyDone; //设置竖直方向对齐方式
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//设置水平方向对齐方式
//textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
textField.delegate = self; UITextField *secondTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, self.view.frame.size.width-40, 50)];
secondTextField.backgroundColor = [UIColor yellowColor];
secondTextField.borderStyle = UITextBorderStyleRoundedRect;
secondTextField.secureTextEntry = YES; [self.view addSubview:secondTextField];
//设置window第一响应者身份
[secondTextField becomeFirstResponder]; //设置viewController作为textField的代理
secondTextField.delegate = self;
[self.view addSubview:textField]; UILabel *label= [[UILabel alloc] initWithFrame:CGRectMake(20, 300, self.view.frame.size.width-40, 50)];
label.backgroundColor = [UIColor grayColor];
label.tag = 100;
[self.view addSubview:label];
} //代码标签
#pragma mark ----UITextFieldDelegate---- - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"将要开始编辑");
return YES;//返回NO不能进行后续的编辑
} - (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"文本开始编辑");
} - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(@"将要结束编辑");
return YES;
} - (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"文本编辑结束");
UILabel *label = (UILabel *)[self.view viewWithTag:100];
label.text = textField.text;
} - (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"returnKey被点击");
//隐藏键盘
[textField resignFirstResponder];
return YES;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
UI2_UITextField的更多相关文章
随机推荐
- Android 手机设置CMWAP 接入点
Android 手机设置CMWAP 接入点 设置--- 无限控件---移动网络设置---接入点名称----再按菜单---选择新apn 第一步,设置CMNET 上网APN 新建 APN 1.名称:cmn ...
- 【JavaScript】JavaScript模块化编程 - CommonJS, AMD 和 RequireJS之间的关系
通行的Javascript模块规范共有两种:CommonJS和AMD 先说说CommonJS CommonJS - 大家是不是觉得JavaScript仅仅是一个客户端的编译语言,其实JavaScr ...
- IDEA和Eclipse经常使用快捷键(Win Mac)
一. 代码生成/补全 Alt+回车 导入包,自己主动修正(Eclipse中Ctrl+Shift+O ) Alt+Inser 生成getter setter.构造器 Ctrl+Shift+Space( ...
- XCODE4.6从零开始添加视图
转自:http://www.cnblogs.com/luoxs/archive/2012/09/23/2698995.html 对于很多初学者来说,肯定希望自己尝试不用傻瓜的“Single View ...
- 《转》Java中HashMap详解
HashMap 和 HashSet 是 Java Collection Framework 的两个重要成员,其中 HashMap 是 Map 接口的常用实现类,HashSet 是 Set 接口的常用实 ...
- IPC——信号
Linux进程间通信——使用信号 一.什么是信号 用过Windows的我们都知道,当我们无法正常结束一个程序时,可以用任务管理器强制结束这个进程,但这其实是怎么实现的呢?同样的功能在Linux上是通过 ...
- stm32f107vc在IAR环境下,引用库函数的工程文件的配置方法
stm32做开发很方便的一个原因是大家可以稍稍放松对于硬件寄存器等的设置,因为stm32有了非常丰富和实用的库函数,外设文件等等,所以我们在使用的时候可以更加关注程序开发的过程和逻辑关系.但是,在开发 ...
- Linux / Unix Command: bunzip2--reference
http://linux.about.com/library/cmd/blcmdl1_bunzip2.htm NAME bzip2, bunzip2 - a block-sorting file co ...
- Java再学习——关于ConcurrentHashMap
ConcurrentHashMap提供了和Hashtable以及SynchronizedMap中所不同的锁机制. 1,在并发方面, ConcurrentHashMap提供了好得多的并发性.多个读操作几 ...
- SpringMvc多文件上传简单实现
public ResponseItem uploadFile(MultipartHttpServletRequest request,FileItem fileItem,PageData pd) { ...