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的更多相关文章
随机推荐
- JS App
JS App 从架构上来看, SPA结构-------RPA结构 不仅仅是JS,还要从Application的角度来看. JS只是单个页面或者功能,Application是从整体的角度来看结构.
- typedef函数指针使用方法
1.简单的函数指针的应用 形式1:返回类型(*函数名)(參数表) char (*pFun)(int); char glFun(int a){ return;} void main() { pFun = ...
- ios开发——实用技术篇&数据保存于恢复
数据保存于恢复 用户操作(输入数据)之后,应用程序退出并且终止之后,当用户再次打开应用的时候还是保持原来的状态 一:在storyBoard中设置恢复标志符 二:在AppDalegate中代理方法 -( ...
- python time模块详解(转)
python 的内嵌time模板翻译及说明 一.简介 time模块提供各种操作时间的函数 说明:一般有两种表示时间的方式: 第一种是时间戳的方式(相对于1970.1.1 00:00 ...
- POJ1651:Multiplication Puzzle(区间DP)
Description The multiplication puzzle is played with a row of cards, each containing a single positi ...
- Find the Clones
Find the Clones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6365 Accepted: 2375 D ...
- 1.5.1 Analyzers,Tokenizers,Filters概述
字段分析器(Analyzers)即用于文档索引也用于查询.一个分析器检查字段的文本,并生成一个token流.分析器可能是一个单独的类,也可能是一系列的tokenizer和filter的组合. 分词器把 ...
- 日本电商Rakuten:最凶猛的国际扩张者
这是普及社(puji-she)第五篇关于电商开放平台的文章,今天关注日本的Rakuten,它是一家综合性的企业,核心业务包括电子商务.旅游.信用 及支付.金融证券.新闻门户等.本文关注Rakuten的 ...
- 【Open Search产品评测】- 来往,7天轻松定制属于自己的搜索引擎
[Open Search产品评测]-- 来往,7天轻松定制属于自己的搜索引擎 [使用背景] 相信很多人都遇到过要给网站或者app做一个搜索功能的需求,很久之前自己折腾过lucene,搞了很久, ...
- ARC 和 MRC 小结
ARC 和 MRC 内存管理 从 MRC—>ARC 就是将内存管理部分,从开发者的函数中转移到函数外部的runtime 中.由于 runtime 的开发简单,逻辑层次高,所以 runtime 的 ...