转载自:http://blog.csdn.net/weisubao/article/details/39609579

(1)可以根据需要设置文本框的样式(包括形状、边框颜色、背景等)。
(2)可以根据需要设置文字显示样式(包括输入密码时的密文显示、文字横向居中、纵向居中上下、输入的文字是否首席木大写、文字超过后是否缩小还是向右滚动等)。
(3)可以根据需要设置各种不同的键盘样式(只有数字、只有字母等等)。
(4)还有inputView可以弹出一个视图,用于取代弹出键盘,暂时不知道什么用处,但貌似可以用得地方很多啊。
(5)还有return的样式设置,可以设置为Google也可以设置为Go和Search等更形象的按钮。
(6)还有一个clearsOnBeginEditing是否设置清除按钮也很常用。
(7)还有用得比较多得估计是左右视图,也就是我们常见的用户名和密码的前面还有一个小icon图片表示用户的“小人”和表示密码的“锁”的图片,用左右视图可以加载进来,当然最后要记得设置左右视图模式为Always,不然默认是Never不显示的。
[objc] view plaincopy
  1. - (void)viewDidLoad {
  2. //textfiled1本想是textField1的,但不影响
  3. UITextField *textFiled1=[[UITextField alloc]init];
  4. //此时textField1已存在,但因为是透明背景,所以看不见,但是点击那块地方会发现光标闪烁可写
  5. //为了证明是透明背景而不是白色背景,我们可以设置self.view背景为红色,看看textField1是白色还是透明色
  6. //    self.view.backgroundColor=[UIColor redColor];
  7. textFiled1.frame=CGRectMake(10, 30, 300, 30);
  8. //设置边框样式
  9. //UITextBorderStyleRoundedRect-圆角矩形,背景是白色,不再是透明的
  10. //UITextBorderStyleLine-矩形,黑色边框,透明背景
  11. //UITextBorderStyleBezel-和上面类似,但是是灰色的边框,背景透明
  12. textFiled1.borderStyle=UITextBorderStyleRoundedRect;
  13. //设置背景颜色,会覆盖上面圆角矩形默认的白色背景
  14. textFiled1.backgroundColor=[UIColor purpleColor];
  15. //设置提示(默认)文字
  16. textFiled1.placeholder=@"请输入您的密码";
  17. //设置密文输入,就是和输入密码时类似的显示为小圆点
  18. textFiled1.secureTextEntry=YES;
  19. //设置键盘样式,比如银行取款密码只需要数字,有的输入邮箱需要@等等
  20. //UIKeyboardTypeAlphabet和UIKeyboardTypeDefault类似,就是我们平时看到那样,都是字母,然后有个按键可以切换符号
  21. //UIKeyboardTypeASCIICapable好像和上面差不多
  22. //UIKeyboardTypeDecimalPad,UIKeyboardTypeNumberPad都是数字,但前者多了一个“小数点”按键
  23. //UIKeyboardTypeEmailAddress-除了字母还有小数点和@出现
  24. //UIKeyboardTypeNamePhonePad-貌似正常
  25. //UIKeyboardTypePhonePad-电话键盘,不仅有数字还有*和#的那种
  26. //UIKeyboardTypeNumbersAndPunctuation-只有数字和标点符号
  27. //UIKeyboardTypeTwitter-除了字母还有@和#,这是微博的符号
  28. //UIKeyboardTypeURL-除字母,还有.com按钮,方便输入
  29. //UIKeyboardTypeWebSearch-主要区别在于return键变成了GO键
  30. //注意:如果是最xcode6下的模拟器的话,默认是不调出软键盘的,按CMD+K可以调出,或者在菜单Hardware里地Keyboard里设置
  31. textFiled1.keyboardType=UIKeyboardTypeWebSearch;
  32. //设置键盘外观
  33. //UIKeyboardAppearanceDark和UIKeyboardAppearanceAlert都是把键盘背景变成半透明灰色区别不明显
  34. //UIKeyboardAppearanceLight貌似和UIKeyboardAppearanceDefault一样,没啥区别
  35. textFiled1.keyboardAppearance=UIKeyboardAppearanceAlert;
  36. //设置弹出视图,inputView即弹出的不是键盘而是这个视图
  37. //设置的frame时,只有高度有用,其他x和y和宽都是无效的,宽是默认的整个键盘宽度
  38. UIImageView *imgView1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logo-60@3x.png"]];
  39. imgView1.frame=CGRectMake(60, 60, 300, 300);
  40. textFiled1.inputView=imgView1;
  41. //设置左视图,就是用户名和密码,有时候放个图片的位置
  42. UIView *view1=[[UIView alloc]init];
  43. //x和y无效,x都是0,而y是根据高度来自动调整的。即高度如果超过textField则默认是textField高,如小于textField高度,则上下居中显示。唯一有效的就是宽度
  44. view1.frame=CGRectMake(10, 500, 50, 10);
  45. view1.backgroundColor=[UIColor orangeColor];
  46. textFiled1.leftView=view1;
  47. //最重要的时:默认它是不显示的即UITextFieldViewModeNever,我们可以设置永远显示UITextFieldViewModeAlways
  48. //UITextFieldViewModeUnlessEditing-一开始就有,点击框,呃,貌似还有
  49. //UITextFieldViewModeWhileEditing-一开始没有,点击框就出现
  50. textFiled1.leftViewMode=UITextFieldViewModeAlways;
  51. //同样,我们可以设置右视图,当然也可以加载和图片进来
  52. UIImageView *imgView2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logo-60@3x.png"]];
  53. imgView2.frame=CGRectMake(10, 500, 50, 10);
  54. textFiled1.rightView=imgView2;
  55. textFiled1.rightViewMode=UITextFieldViewModeAlways;
  56. //设置清除按钮,就是那个叉叉X,一点击整个输入框的文字全部删除重新输入的那个X(我们先注释掉不让右视图显示,来查看效果)
  57. //其实我们在写clearButtonMode是它又提示说这是一个UITextFieldViewMode类型,所以也是和上面一样
  58. textFiled1.clearButtonMode=UITextFieldViewModeWhileEditing;
  59. //再次编辑时是否清空内容,这个除特定场景外很少用,会让用户抓狂的
  60. //当然为了模拟再次编辑,我们需要鼠标点到其他地方然后再点回来,所以再创建一个textField
  61. textFiled1.clearsOnBeginEditing=NO;
  62. //这个clearsOnInsertion貌似点击回去再次编辑时不清楚,但是只要一输入内容就会清除之前的
  63. textFiled1.clearsOnInsertion=YES;
  64. UITextField *textField2=[[UITextField alloc]init];
  65. textField2.frame=CGRectMake(10, 80, 300, 100);
  66. textField2.borderStyle=UITextBorderStyleRoundedRect;
  67. [self.view addSubview:textField2];
  68. //我们用上面创建的textField2来做如下
  69. //纵向对齐方式,默认是居中
  70. //UIControlContentVerticalAlignmentCenter居中,所以Top、Bottom就是居上居下。Fill貌似和Top差不多
  71. textField2.contentVerticalAlignment=UIControlContentVerticalAlignmentFill;
  72. //当然还有横向对齐
  73. //也有左中右和Fill四种,但是貌似没看到什么效果,可能对文字无效,因为有专门的针对文字的设置
  74. textField2.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
  75. //设置文字对齐方式
  76. //同样我们输入textAlignment时有提示是NSTextAlignment类型,有好几种,不细讲
  77. textField2.textAlignment=NSTextAlignmentCenter;
  78. //设置调整文字大小以适配宽度(即输入不下时缩小文字,实在缩小不了了,就向后滚动),默认是向右滚动的
  79. textField2.adjustsFontSizeToFitWidth=YES;
  80. //设置最小字号,和上面有关,即小于这个字号的时候,我就不缩小了,直接向右滚动
  81. textField2.minimumFontSize=2;
  82. //设置字母大小样式,输入autocapitalizationType时有提示是UITextAutocapitalizationType类型
  83. //UITextAutocapitalizationTypeAllCharacters-所有字母大写(用键盘输入的话发现失效,需要用软键盘输入才有效,以下同理)
  84. //UITextAutocapitalizationTypeWords-单词首字母大写
  85. //UITextAutocapitalizationTypeSentences-句首字母大写
  86. textField2.autocapitalizationType=UITextAutocapitalizationTypeSentences;
  87. //设置return样式,有Done/Go/Next/Join/Google/Search/Yahoo/EmergencyCall/Send等,除了默认外,其他的按钮都是蓝颜色背景
  88. textField2.returnKeyType=UIReturnKeyEmergencyCall;
  89. [self.view addSubview:textFiled1];
  90. [super viewDidLoad];
  91. // Do any additional setup after loading the view, typically from a nib.
  92. }
[摘要:1.建立 01.UITextField* myTextField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)]; 2.设置托付 01.myTextField.delegate = self;//托付类须要恪守UITextFieldDelegate协定 3. 设置属性 扩大属性 UIControl属性]

1.创建
01.UITextField* myTextField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];  
2.设置委托
01.myTextField.delegate = self;//委托类需要遵守UITextFieldDelegate协议  

3.设置属性

扩展属性

UIControl属性对UITextField完全可以用,下面的都是UITextField扩展的属性

01.myTextField.textAlignment = UITextAlignmentLeft;//默认就是左对齐,这个是UITextField扩展属性

02.myTextField.borderStyle = UITextBorderStyleBezel;//默认是没有边框,如果使用了自定义的背景图片边框会被忽略掉  
03.myTextField.placeholder = @"请在此输入账号";//为空白文本字段绘制一个灰色字符串作为占位符  
04.myTextField.clearsOnBeginEditing = YES;//设置为YES当用点触文本字段时,字段内容会被清除  
05.myTextField.adjustsFontSizeToFitWidth = YES;//设置为YES时文本会自动缩小以适应文本窗口大小。默认是保持原来大小,而让长文本滚动  
06.//myTextField.background = [UIImage imageNamed:@"registBtn"];//可以接受UIImage对象,此项设置则边框失效。  
07.myTextField.clearButtonMode = UITextFieldViewModeUnlessEditing;//右边显示的'X'清楚按钮  
08.//myTextField.LeftView =  
09.//myTextField.leftViewMode =   
10.//myTextField.RightView =  
11.//myTextField.rightViewMode =    
4.下列方法在创建一个UITextField的子类时可以重写:borderRectForBounds指定矩形边界textRectForBounds 指定显示文本的边界placeholderRectForBounds指定站位文本的边界editingRectForBounds指定编辑中文本的边界clearButtonRectForBounds指定显示清除按钮的边界leftViewRectForBounds指定显示左附着视图的边界rightViewRectForBounds指定显示右附着视图的边界委托方法。
========================================
01.- (CGRect)clearButtonForBounds:(CGRect)bounds{  
02.    return CGRectMake(bounds.origin.x +bounds.size.width-50,   
03.                      bounds.origin.y+bounds.size.height-20, 16, 16);  
04.}  
========================================
01.- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{  
02.    //返回一个BOOL值,指定是否循序文本字段开始编辑  
03.    return YES;  
04.}  
========================================
01.- (void)textFieldDidBeginEditing:(UITextField *)textField{  
02.    //开始编辑时触发,文本字段将成为first responder  
03.}  
========================================
01.- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{  
02.    //返回BOOL值,指定是否允许文本字段结束编辑,当编辑结束,文本字段会让出first responder  
03.    //要想在用户结束编辑时阻止文本字段消失,可以返回NO  
04.    //这对一些文本字段必须始终保持活跃状态的程序很有用,比如即时消息  
05.    return NO;  
06.}  
========================================
01.- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{  
02.    //当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。  
03.    //这对于想要加入撤销选项的应用程序特别有用  
04.    //可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。     
05.    //要防止文字被改变可以返回NO  
06.    //这个方法的参数中有一个NSRange对象,指明了被改变文字的位置,建议修改的文本也在其中  
07.    return YES;  
08.}  
========================================
01.- (BOOL)textFieldShouldClear:(UITextField *)textField{  
02.    //返回一个BOOL值指明是否允许根据用户请求清除内容  
03.    //可以设置在特定条件下才允许清除内容  
04.    return YES;  
05.}  
========================================
01.-(BOOL)textFieldShouldReturn:(UITextField *)textField{  
02.    //返回一个BOOL值,指明是否允许在按下回车键时结束编辑  
03.    //如果允许要调用resignFirstResponder 方法,这回导致结束编辑,而键盘会被收起  
04.    [textField resignFirstResponder];//查一下resign这个单词的意思就明白这个方法了  
05.    return YES;  
06.}  
========================================
虚拟键盘挡住UITextField时的解决方法:
RootViewController.h 中:
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController<UITextFieldDelegate> {
UITextField *textField1;
UITextField *textField2;
}
@property (nonatomic,retain) UITextField *textField1;
@property (nonatomic ,retain) UITextField *textField2;
-(IBAction)backgroundTap:(id)sender;
@end
RootViewController.m 中:
#import "RootViewController.h"
@implementation RootViewController
@synthesize textField1;
@synthesize textField2;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
UIView *back = [[UIView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
back.backgroundColor = [UIColor grayColor];
self.view = back;
[back release];
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
UIControl *_back = [[UIControl alloc] initWithFrame:self.view.frame];
_back.backgroundColor = [UIColor grayColor];
self.view = _back;
[_back release];
[(UIControl *)self.view addTarget:self action:@selector(backgroundTap:) forControlEvents:UIControlEventTouchDown];
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 200, 30)];
textField1.backgroundColor = [UIColor clearColor];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.textColor = [UIColor redColor];
textField1.delegate = self;
[self.view addSubview:textField1];
textField2 = [[UITextField alloc] initWithFrame:CGRectMake(20, 30, 200, 30)];
textField2.backgroundColor = [UIColor clearColor];
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.textColor = [UIColor redColor];
textField2.delegate = self;
[self.view addSubview:textField2];
}
#pragma mark -
#pragma mark 解决虚拟键盘挡住UITextField的方法
- (void)keyboardWillShow:(NSNotification *)noti
//键盘输入的界面调整 
//键盘的高度
float height = 216.0; 
CGRect frame = self.view.frame; 
frame.size = CGSizeMake(frame.size.width, frame.size.height - height); 
[UIView beginAnimations:@"Curl"context:nil];//动画开始 
[UIView setAnimationDuration:0.30]; 
[UIView setAnimationDelegate:self]; 
[self.view setFrame:frame]; 
[UIView commitAnimations];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
// When the user presses return, take focus away from the text field so that the keyboard is dismissed. 
NSTimeInterval animationDuration = 0.30f; 
[UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
[UIView setAnimationDuration:animationDuration]; 
CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height); 
self.view.frame = rect;
[UIView commitAnimations];
[textField resignFirstResponder];
return YES; 
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
CGRect frame = textField.frame;
int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
NSTimeInterval animationDuration = 0.30f; 
[UIView beginAnimations:@"ResizeForKeyBoard" context:nil]; 
[UIView setAnimationDuration:animationDuration];
float width = self.view.frame.size.width; 
float height = self.view.frame.size.height; 
if(offset > 0)
{
CGRect rect = CGRectMake(0.0f, -offset,width,height); 
self.view.frame = rect; 
[UIView commitAnimations]; 
}
#pragma mark -
#pragma mark 触摸背景来关闭虚拟键盘
-(IBAction)backgroundTap:(id)sender
{
// When the user presses return, take focus away from the text field so that the keyboard is dismissed. 
NSTimeInterval animationDuration = 0.30f; 
[UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
[UIView setAnimationDuration:animationDuration]; 
CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height); 
self.view.frame = rect;
[UIView commitAnimations];
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
}
#pragma mark -
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[textField1 release];
[textField2 release];
[super dealloc];
}
RootViewController.m 中的backgroundTap:方法,用来实现触摸背景来关闭虚拟键盘。
这个方法用的时候首先把RootViewController上的view改成UIControl,然后通过UIControl的事件UIControlEventTouchDown来触发上面的方法backgroundTap: 。
注意下面的代码:
UIControl *_back = [[UIControl alloc] initWithFrame:self.view.frame];
_back.backgroundColor = [UIColor grayColor];
self.view = _back;
[_back release];
[(UIControl *)self.view addTarget:self action:@selector(backgroundTap:) forControlEvents:UIControlEventTouchDown];
解决textField被键盘挡住的问题的方法有三个:
- (void)keyboardWillShow:(NSNotification *)noti;//调整虚拟键盘与self.view之间的关系。
-(BOOL)textFieldShouldReturn:(UITextField *)textField;//触摸键盘上的return键时关闭虚拟键盘
- (void)textFieldDidBeginEditing:(UITextField *)textField;//当编辑文本的时候,如果虚拟键盘挡住了textField,整个view就会向上移动。移动范围是一个键盘的高度216。

转载于:https://www.cnblogs.com/Always-LuoHan/p/5267317.html

UI   控件 —UITextFile相关推荐

  1. iOS SwiftUI篇-2 UI控件 Text Button Image List

    iOS SwiftUI篇-2 UI控件 Text Button Image List Text 显示文本,相当于UILabel import SwiftUIstruct TextContentView ...

  2. UI控件无法响应点击等事件的探索

    2019独角兽企业重金招聘Python工程师标准>>> UI控件无法响应点击等事件的探索 一.响应者链 关于响应者链,有如下一段介绍:每一个应用有一个响应者链,我们的视图结构是一个N ...

  3. RxSwift UI控件扩展

    RxSwift UI控件扩展 最好的示例是参考RxCocoa查看类似的属性如何扩展Rx化的. 为了配合RxSwift的绑定关系,RxCocoa提供简单的基于Cocoa控件的扩展,但是很少,比如Labe ...

  4. UI控件库分享:DWZ(j-UI)、LigerUI、Linb

    DWZ(j-UI): 在线演示地址:http://demo.dwzjs.com 在线文档:http://demo.dwzjs.com/doc/dwz-user-guide.pdf DWZ框架Ajax开 ...

  5. iOS 使用UI控件的外观协议UIAppearance进行设置默认UI控件样式

    在iOS开发中,经常会对UINavigationBar的样式进行全局样式.采用的设置方式有两种: 第一种,采用方式如下: [UINavigationBar appearance] 这种是对一类对象的默 ...

  6. 使用ExtJs创建新的UI控件(转)

    组合或扩展 当创建一个新类,往往要作出这么的一个选择:要么拥有某个工具类的实例来扮演首要的角色,要么扩展那个类. 使用ExtJs过程中,推荐从最靠近的基类开始扩展,实现所需的功能即可.这是因为Ext提 ...

  7. 【IOS 开发】基本 UI 控件详解 (UIDatePicker | UIPickerView | UIStepper | UIWebView | UIToolBar )

    转载注明出处 : http://blog.csdn.net/shulianghan/article/details/50348982 一. 日期选择器 (UIDatePicker) UIDatePic ...

  8. 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)

    博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ...

  9. Windows Phone UI控件

    培训第一章XAML介绍属性设置设置方式:1: 特性设置2: 使用属性元素属性继承XAML中的颜色和画刷Content属性资源定义和访问共享机制 画刷 外观 文字等Style使用与继承Style是属性集 ...

最新文章

  1. RabbitMQ 异常与任务分发
  2. Python设计模式-职责链模式
  3. 合作开发用到的几个 设计模式
  4. 如何写windbg高级脚本---以访问文件的windbg脚本为例说明
  5. 五步让你玩转CocoaPods
  6. springboot:SpringBoot项目启动成功,但无法访问且提示404
  7. c++ sort 从大到小排序_算法的艺术:MySQL order by对各种排序算法的巧用
  8. 【VRP】基于matlab改进的模拟退火和遗传算法求解车辆路径规划问题【含Matlab源码 343期】
  9. 「快手极速版」榜 App Store 总榜第一
  10. OpenCV——图像矩
  11. jQuery写法 入口函数
  12. 毕业5年决定人的一生(大家有空一定要看看)
  13. 2.Python环境搭建
  14. python基础-廖雪峰
  15. (等倾、等厚干涉)MATLAB在迈克尔逊干涉仪中的应用
  16. python中整数的长度_Python中正整数的位长度
  17. 我们是如何测试360手机浏览器的 –360手机浏览器测试范围概述
  18. Python | 阿尔法基本图形绘制
  19. 【算法】哈夫曼压缩算法-学习记录
  20. css的文本省略号(单行和多行)

热门文章

  1. qt 操作html,如何在Webkit窗口中操作页面内容(使用QT和QTWebKit)?
  2. CSDN创始人蒋涛:拥抱中国开源技术生态发展黄金十年
  3. 【蓝桥杯Java_C组·从零开始卷】第八节、综合测试
  4. python的时间差计算
  5. 游戏设计、原型与开发:基于Unity与C#从构思到实现pdf
  6. C# string类型和byte[]类型相互转换
  7. 在pl/sql中使用exp/imp工具实现oracle数据导出/导入
  8. 获取需要登陆才能被访问的页面,HttpClient(扩展HttpWebRequest)来实现
  9. python抓取数据库_Python-7.爬取大量数据存入数据库
  10. layui表格更改一列数据_layui数据表格隐藏列的方法介绍