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

随机推荐

  1. 深入研究Clang(三) libclang

    作者:史宁宁 如今的Clang,不不过一个编译器前端,同一时候也能够作为一个库使用.作为一个库使用的时候,能够用它去分析C/C++/ObjectC语言代码,能够分析源代码得到AST,也能够获取已经分析 ...

  2. 字符集乱码问题:ISO-8859-1和GBK

    问题,引用百度知道的问题吧: http://zhidao.baidu.com/question/51342167.html?qbl=relate_question_0&word=%C3%84% ...

  3. Citrix 服务器虚拟化之二十七 XenApp6.5发布服务器桌面

    Citrix 服务器虚拟化之二十七  XenApp6.5发布服务器桌面 XenApp可发布以下类型的资源向用户提供信息访问,这些资源可在服务器或桌面上虚拟化: 1)  服务器桌面:发布场中服务器的整个 ...

  4. iOS执行时工具-cycript

    cycript是大神saurik开发的一个很强大的工具,能够让开发人员在命令行下和应用交互,在执行时查看和改动应用.它确实能够帮助你破解一些应用,但我认为这个工具主要还是用来学习其它应用的设计(主要是 ...

  5. JavaScript跨域实现

    最近在做个上传文件的服务,其中包含一个上传的web页面.目的是想客户端页面嵌套这个web页面,然后直接将文件上传到服务器. 因为文件不同所以需要保存到的文件夹名称也不一样,所以客户端需要传递一个文件夹 ...

  6. Linux根文件系统的制作

    转载:http://www.cnblogs.com/hnrainll/archive/2011/06/09/2076655.html 1. 根文件系统 文件系统是包括在一个磁盘(包括光盘.软盘.闪盘及 ...

  7. EF——继承映射关系TPH、TPT和TPC的讲解以及一些具体的例子 05 (转)

    EF里的继承映射关系TPH.TPT和TPC的讲解以及一些具体的例子   本章节讲解EF里的继承映射关系,分为TPH.TPT.TPC.具体: 1.TPH:Table Per Hierarchy 这是EF ...

  8. IOS UIwebView 加载网络图片 使用相对地址

    方法一: 在html文件内直接使用file:///user//xx//image.png的绝对路径 注:这样可以显示图片,但是如果在程序目录修改,图片就不能显示 方法二: 在html使用占位符,如:在 ...

  9. Android(java)学习笔记265:Android线程形态之 HandlerThread

    1.  HandlerThread Android HandlerThread 完全解析 Handler与HandlerThread区别,HandlerThread应用(对比AsyncTask) 备注 ...

  10. Java基础知识强化之IO流笔记79:NIO之 SocketChannel

    1. Java NIO中的SocketChannel是一个连接到TCP网络套接字的通道.可以通过以下2种方式创建SocketChannel: 打开一个SocketChannel并连接到互联网上的某台服 ...