寒哥管理的技术专题iOS DeveloperiOS进阶指南

2016-01-10  本文已影响214人  傲骨天成科技

1.初始化
UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(100,100,100,100,100)];
2.设置占位字符
textField.placeholder=@"请输入手机号";
3.设置键盘样式 keyboardType
textField.keyboardType=UIKeyboardTypeNumberPad; 现在是纯数字键盘

// UIKeyboardTypeDefault, 默认的
// UIKeyboardTypeASCIICapable, 显示一个键盘可以输入ASCII字符,非ASCII键盘保持活跃
// UIKeyboardTypeNumbersAndPunctuation, 数字和各种标点符号
// UIKeyboardTypeNumberPad, 数字键盘
// UIKeyboardTypePhonePad, 电话薄(1 - 9 * 0 #,字母的数字)
// UIKeyboardTypeDecimalPad 带小数点的数字键盘
4.设置要输入字体的颜色
textField.textColor=[UIColor redColor];
5.设置边框样式
textField.borderStyle=UITextBorderStyleNone;

UITextBorderStyleNone,    默认   没有边框
UITextBorderStyleLine,      矩形
UITextBorderStyleBezel,    矩形
UITextBorderStyleRoundedRect    角有弧度的矩形

6.给边框下设置一条虚线
边框的高为大于1时上面添加的字符串才可以显示出效果
UILabel lineLable=[[UILabel alloc]initWithFrame:CGRectMake(100, 149, 150, 2)];
当边框背景为白色时,才能凸显出上面字符串的颜色
[lineLable setBackgroundColor:[UIColor whiteColor]];
[lineLable setText:@"- - - - - - - - - - - - -"];
lineLable.textColor=[UIColor blackColor];
[self.window addSubview:lineLable];
7.给某文本框输入字设置安全样式 secure Text Entry安全的文本输入
textField.secureTextEntry=YES;
8.设置textField能不能输入的属性
textField.enabled=YES;
textField.enabled=NO;
9.设置右侧小叉号(枚举值) clear Button Mode清除内容的按钮的模式
textField.clearButtonMode=UITextFieldViewModeAlways; 一直显示
// UITextFieldViewModeNever, 不显示出来,也就是此时没有此键
// UITextFieldViewModeWhileEditing, 当textField处于编辑状态时,此键显示
// UITextFieldViewModeUnlessEditing, 当编辑完成时,此键显示
10.首字母大写(暂时不会用)
textField.autocapitalizationType ;
11.给编辑框设置 左视图(以像素大小30
30的为例)

  直接给相框内放置图片     图片的大小就是相框的大小
  UIImageView  *imageView=[ [UIImageView    alloc]initWithImage:[UIImage     imageNamed:@"左视图.jpg"] ];
   将相框放到编辑框的左侧
   textField.leftView=imageView;
   设置显示模式                        后面的与条目9是一样的,四种模式
   textField.leftViewMode=UITextFieldViewModeAlways;

12.给编辑框设置 右视图(以像素大小30*30的为例)
方法同上,只是left改成right

13.代理方法 设置点击按钮,则按钮上的图片或者文字返回到输入框中
给上面的textField设置tag值
textField.tag=2000;
使得textField称为代理 因此要导入代理协议
textField.delegate=self; 在此工程的.h文件中导入UITextFieldDelegate协议

   在按钮的方法中设置点击事件
   //得到输入框                                                                        
   UITextField  *myTextField=(UITextField *)[self.window       viewWithTag:2000];
  //得到标题                              如果按钮上是图片,则是imageForState
  NSString   *title=[sender      titleForState:UIControlStateNormal];
  //将标题显示在输入框      并且拼接字符串
   myTextField.text=[myTextField     stringByAppendingString: title];

14.回收键盘 取消第一响应者
[myTextField resignFirstResponder];
15.通过点击系统键盘的return 回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

16.自定义键盘
设置键盘的大小区域
UIView *keyBoardView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,
CGRectGetWidth(self.window.frame),256)];
键盘的区域与所要关联的输入框
myTextField.inputView=keyBoardView;
17.协议代理方法:UITextFieldDelegate 所有的协议方法都是可选的(optional)
流程:
a.-(BOOL)textFieldShouldBeginEditing:
[UITextField *]textField; 是否开始编辑状态
b.-(void)textFieldDidBeginEditing:(UITextField *)textField 已经开始编辑状态
c.-(BOOL)textFieldShouldEndEditing:(UITextField *)是否结束编辑状态
d.-(void)textFieldDidEndEditing:(UITextField *)textField 已经结束编辑状态
e.-(BOOL)textFieldShouldReturn:(UITextField *) //点击return按钮所执行的代理方法

18.改变占位字符的文字颜色
textField.placeholder = @"username is in here!";
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

19.输入文字时执行的代理方法

上一篇 下一篇

猜你喜欢

热点阅读