//
// 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. 图片流Base64编码 转图片

    using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Web; ...

  2. [Angular2 Router] Understand the Angular 2 Base href Requirement

    The <base href=”/”/> you define will determine how all other assets you plan on loading treat ...

  3. java实现迷宫算法--转

    沿着所有方向进行探测,有路径则走,没有路径则从栈中回退. 回溯法是一种不断试探且及时纠正错误的搜索方法,下面的求解过程采用回溯法.从入口出发,按某一方向向前探索,若能走通(未走过的),即某处可以到达, ...

  4. iOS iphone5屏幕适配 autosizing

    转自:http://blog.sina.com.cn/s/blog_a843a8850101jxhh.html iphone5出来了,从不用适配的我们也要像android一样适配不同分辨率的屏幕了. ...

  5. java中十进制转二进制转换函数

    十进制转成十六进制: Integer.toHexString(int i) 十进制转成八进制 Integer.toOctalString(int i) 十进制转成二进制 Integer.toBinar ...

  6. 关于 ArtifactTransferException: Failure to transfer

    eclipse 在导入maven project后,pom.xml有可能出现这种错误. 这里update maven project解决了:右键点击Maven项目->Maven->Upda ...

  7. NET开发必备工具之-LINQPad

    第一步,下载:http://www.linqpad.net/ 第二步,安装 第三步,打开LINQPad 第四步,添加链接 第五步,输入SQL Server,用户名,密码 第六步,点击OK,成功链接 第 ...

  8. B - Fill

    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UV ...

  9. WWH学习模式

    WWH是"What+Why+How"的简称,是对学习方法最完美的概括."如果不按照WWH这种模式来教学,90%的结果是老师没教好,学生学不好." 1.What( ...

  10. Android SQLite 的简单实例

    1.前言: 今天再一次去蹭了一下某老师的android课,这一次讲的是Android的SQLite的使用,老师当场讲解了他自己做的例子. 回来之后,我春心萌动,不得不拿着参考资料再做了一个类似的例子, ...