ios开发学习路线|iOS开发学习 UITextField介绍使用教程

更新时间:2019-12-07    来源:ios    手机版     字体:

【www.bbyears.com--ios】

一、关于UITextField的介绍

1)概念: UITextField 是用于接收用户输入的一个控件

2)UITextField  初始化实例代码:

//创建一个UItextField实例
UITextField *textField = [[UITextField alloc] init];
textField.frame = CGRectMake(10, 40, self.view.frame.size.width - 20, 40);
textField.backgroundColor = [UIColor lightGrayColor];
//设置textFiled中的文字
textField.text = @"用户名";
//设置textFiled的字体
textField.font = [UIFont systemFontOfSize:20];
//设置textFiled的文字颜色
textField.textColor = [UIColor purpleColor];
[self.view addSubview: textField];

 
3)textAlignment 设置文本的对齐方式 例如:

//设置textFiled的文本左对齐
textField.textAlignment = NSTextAlignmentLeft;

4)borderStyle 设置文本框样式  例如:

//设置textFiled样式为无边框样式
    textField.borderStyle = UITextBorderStyleNone;
   //borderStyle 有以下几种类型
   //1、UITextBorderStyleNone,
   //2、UITextBorderStyleLine,
   //3、UITextBorderStyleBezel,
   //4、UITextBorderStyleRoundedRect

 
5)layer.cornerRadius 设置文本框圆角 例如:

textField.layer.cornerRadius = 4.0f;

6)layer.borderWidth 设置文本框边框宽度  例如:

textField.layer.borderWidth = 1;

7)layer.borderColor 设置文本框边框颜色 例如:

textField.layer.borderColor = [UIColor darkGrayColor].CGColor;
 
8)background 设置文本框背景图片 例如:

UIImage *image = [UIImage imageNamed:@"btnEmojBtn"];
textField.background = image;
 
9)设置文本框默认显示文字(默认灰色字体点击键盘输入时文字消失)例如:

方法一:placeholder

textField.placeholder = @"用户名";

方法二: NSMutableAttributedString

NSMutableAttributedString *muAttStr = [[NSMutableAttributedString alloc] initWithString:@"用户名"];
[muAttStr addAttribute:NSForegroundColorAttributeName value:
[UIColor blueColor]    range:NSMakeRange(0, muAttStr.length)];
textField.attributedPlaceholder = muAttStr;


 
10)clearButtonMode 设置文本框的清除内容按钮显示模式(即文本框右边的小叉) 例如:

textField.clearButtonMode = UITextFieldViewModeWhileEditing;
typedef enum {
    UITextFieldViewModeNever, //从不出现
    UITextFieldViewModeWhileEditing, //编辑时出现
    UITextFieldViewModeUnlessEditing,  //除了编辑外都出现
    UITextFieldViewModeAlways   //一直出现
} UITextFieldViewMode;

 
11)leftView 设置文本框的左边视图 例如:

UIView *leftView = [[UIView alloc] init];
 leftView.backgroundColor = [UIColor clearColor];
 leftView.frame = CGRectMake(0, 0, 50, 40);
 textField.leftView = iconImgView;
 //设置文本框左边视图的出现方式
 textField.leftViewMode = UITextFieldViewModeAlways;


12)returnKeyType 设置文本框呼出的键盘右下角按钮类型 例如:

textField.returnKeyType = UIReturnKeyDone;

13)userInteractionEnabled 设置文本框是否可编辑  例如:

//设置文本框不可编辑
textField.userInteractionEnabled=NO;

14)becomeFirstResponder 设置文本框成为第一响应者即呼出键盘 例如:

[textField becomeFirstResponder];
//注意:当设置一个控件为第一响应者之后,再设置其他为第一响应者无效 第一响应者有且只能有一个
 

15)resignFirstResponder 设置文本框放弃第一响应者即隐藏键盘 例如:

[textField resignFirstResponder];


16)secureTextEntry 设置文本框密文模式即密码框 例如:

textField .secureTextEntry = YES;

 
17)keyboardType 设置文本框呼出键盘类型 例如:

//设置数字键盘
textField.keyboardType = UIKeyboardTypeNumberPad;
//keyboardType 有:
typedef enum {
    UIKeyboardTypeDefault,      // 默认键盘,支持所有字符         
    UIKeyboardTypeASCIICapable,  //支持ASCII的默认键盘
    UIKeyboardTypeNumbersAndPunctuation,  //标准电话键盘,支持+*#字符
    UIKeyboardTypeURL,            //URL键盘,支持.com按钮 只支持URL字符
    UIKeyboardTypeNumberPad,   //数字键盘
    UIKeyboardTypePhonePad,    // 电话键盘
    UIKeyboardTypeNamePhonePad,   //电话键盘,也支持输入人名
    UIKeyboardTypeEmailAddress,   //用于输入电子 邮件地址的键盘     
    UIKeyboardTypeDecimalPad,     //数字键盘 有数字和小数点
    UIKeyboardTypeTwitter,        //优化的键盘,方便输入@、#字符
    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;


18)adjustsFontSizeToFitWidth   设置文本框内容超出文本框范围时文字自动变化大小  例如:

textField.adjustsFontSizeToFitWidth = YES;
//设置自动缩小显示的最小字体大小
textField.minimumFontSize = 20;

 

19)keyboardAppearance 设置键盘外观 例如:  

//深灰 石墨色键盘
textField.keyboardAppearance=UIKeyboardAppearanceAlert;
typedef enum {
UIKeyboardAppearanceDefault, //默认外观,浅灰色
UIKeyboardAppearanceAlert,    // 深灰 石墨色 
} UIReturnKeyType;

 

20)文本框 内容对齐方式 例如:  

//内容的垂直对齐方式  UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

 

21)font 设置文本框字体样式和大小 例如:

textField.font = [UIFont fontWithName:@"Arial" size:20.0f];

 
22)delegate 设置文本框代理对象 例如:

textField.delegate = self;
//文本框设置代理时当前类先必须遵守 UITextFieldDelegate 协议然后实现它的协议方法
//它的协议方法有:
 //返回一个BOOL值,是否允许文本框开始编辑 
 1、- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
//开始编辑时触发,文本框将成为第一响应者(first responder )         
2、- (void)textFieldDidBeginEditing:(UITextField *)textField;
//返回BOOL值,是否允许文本框结束编辑,当编辑结束,当前文本框会放弃第一响应者身份         
3、- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
//当文本框结束编辑时触发此方法
4、- (void)textFieldDidEndEditing:(UITextField *)textField;
//当文本款内容改变时触发此方法,可在此方法中对文本内容做非法字符筛选或限制输入
5、- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;    
//当点击文本框右边清除按钮时触发此方法
6、- (BOOL)textFieldShouldClear:(UITextField *)textField;  
//当点击键盘右下角按钮Return 按钮时触发此方法            
7、- (BOOL)textFieldShouldReturn:(UITextField *)textField;




IOS开发 UITextfield控件的所有属性设置与解析


//创建UITextField对象
UITextField * tf=[[UITextField alloc]init];

//设置UITextField的文字颜色
tf.textColor=[UIColor redColor];

//设置UITextField的文本框背景颜色
tf.backgroundColor=[UIColor grayColor];

//设置UITextField的边框的风格
tf.borderStyle=UITextBorderStyleRoundedRect;

//设置UITextField的代理
tf.delegate=self;

//设置UITextField的文字对齐方式
tf.textAlignment=UITextAlignmentCenter;//居中对齐
tf.textAlignment=UITextAlignmentLeft;//左对齐
tf.textAlignment=UITextAlignmentRight;//右对齐
tf.textAlignment=UITextAlignmentFill;//填充对齐

//设置UITextField的文字大小和字体
tf.font=[UIFont fontWithName:@"Times New Roman" size:20];

//设置UITextField自适应文本框大小
tf.adjustsFontSizeToFitWidth=YES/NO;//自适应宽度
tf.adjustsFontSizeToFitHeight=YES/NO;//自适应高度

//设置UITextField是否拥有一键清除的功能
tf.clearsOnBeginEditing=YES/NO;

//设置一键清除按钮是否出现
tf.clearButtonMode=UITextFieldViewModeNever;

//设置UITextField的初始隐藏文字
tf.placeholder=@"输入密码";

//当UITextField的样式为UITextBorderStyleNone的时候,修改背景图片
tf.background=[UIImage imageNamed:@"xx.png"];

//设置UITextField的左边view
tf.leftView=xxx;

//设置UITextField的左边view出现模式
tf.leftViewMode=UITextFieldViewModeAlways;

//设置UITextField的右边view
tf.rightView=xxx;

//设置UITextField的右边view出现模式
tf.rightViewMode=UITextFieldViewModeAlways;

//设置UITextField的字的摆设方式
tf.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;



iOS UITextField总结

1.UITextField的初始化及设置

 searchTextField=[[UITextField alloc]initWithFrame:CGRectMake(1.0,0.0,searchBackGroundImageView.frame.size.width, searchBackGroundImageView.frame.size.height)];//创建一个UITextField对象,及设置其位置及大小
    searchTextField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;//设置其输入内容竖直居中
    UIImageView* imgV=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"search_ico"]];
    searchTextField.leftView=imgV;//设置输入框内左边的图标
   [self.tf11setClearButtonMode:UITextFieldViewModeWhileEditing];//右侧删除按钮
    searchTextField.leftViewMode=UITextFieldViewModeAlways;
    searchTextField.placeholder=@"请输入关键字";//默认显示的字
    searchTextField.secureTextEntry=YES;//设置成密码格式
    searchTextField.keyboardType=UIKeyboardTypeDefault;//设置键盘类型为默认的
    searchTextField.returnKeyType=UIReturnKeyDefault;//返回键的类型
    searchTextField.delegate=self;//设置委托


2.可以通过委托方法关闭键盘

-(BOOL)textFieldShouldReturn:(UITextField*)textField
{
   [searchTextField resignFirstResponder];
  return YES;
}


 3.聚焦方法

 [searchTextField becomeFirstResponder];


4.各种收键盘

- (void)viewDidLoad {
    [superviewDidLoad];
    //第一种:触摸背景来关闭虚拟键盘
    //给背景view添加UIControl触发点击事件
    UIControl *_back = [[UIControlalloc] initWithFrame:self.view.frame];
    _back.backgroundColor = [UIColorgrayColor];
    self.view = _back;
    [_back release];
    [(UIControl *)self.viewaddTarget:selfaction:@selector(backgroundTap) forControlEvents:UIControlEventTouchDown]; 
    //红色textfield
    textField1 = [[UITextFieldalloc] initWithFrame:CGRectMake(20, 300, 200, 30)];
    textField1.backgroundColor = [UIColorredColor];
    textField1.borderStyle = UITextBorderStyleRoundedRect;
    textField1.textColor = [UIColorredColor];
    textField1.delegate = self;
    [self.viewaddSubview:textField1];
    
    textField2 = [[UITextFieldalloc] initWithFrame:CGRectMake(20, 30, 200, 30)];
    textField2.backgroundColor = [UIColorclearColor];
    textField2.borderStyle = UITextBorderStyleRoundedRect;
    textField2.textColor = [UIColorredColor];
    textField2.delegate = self;
    [self.viewaddSubview:textField2];
    
    //第三种: 点击虚拟键盘上按键手键盘
    UIToolbar *bar=[[UIToolbaralloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    UIBarButtonItem *btn=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCanceltarget:selfaction:@selector(resign)];
    UIBarButtonItem *space=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:selfaction:nil];//用于把btn挤到右侧
    NSArray *btarr=[NSArrayarrayWithObjects:btn,space,nil];
    bar.items=btarr;
    //    bar.tintColor=[UIColor blackColor];//给toolbar染色作用同下
    bar.barStyle=UIBarStyleBlackTranslucent;//bar的类型设置半透明
    self.textField1.inputAccessoryView=bar;
    self.textField2.inputAccessoryView=bar;
}
//toolbar方法
-(void)resign{
    [self.textField1resignFirstResponder];
    [self.textField2resignFirstResponder];
}
//点击北京手键盘
-(IBAction)backgroundTap
{     
    //动画是执行推上textfield后还原
    NSTimeInterval animationDuration = 0.30f;       
    [UIViewbeginAnimations:@"ResizeForKeyboard"context:nil];       
    [UIViewsetAnimationDuration:animationDuration];       
    CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);      //还原view 
    self.view.frame = rect;
    [UIViewcommitAnimations];
    
    [textField1resignFirstResponder];
    [textField2resignFirstResponder];
}
 
/**第二种:点击return键手键盘**/
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{       
    //动画是执行推上textfield后还原
    NSTimeInterval animationDuration = 0.30f;       
    [UIViewbeginAnimations:@"ResizeForKeyboard"context:nil];       
    [UIViewsetAnimationDuration:animationDuration];       
    CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);      //还原上一步view上提效果
    self.view.frame = rect;
    [UIViewcommitAnimations];
    
    [textField resignFirstResponder];
    returnYES;       
}
/***解决虚拟键盘挡住UITextField的方法,将整个self.view上推键盘顶部与textfield底部大小的距离即offset***/
- (void)textFieldDidBeginEditing:(UITextField *)textField
{      
    CGRect frame = textField.frame;
    int offset = frame.origin.y+60- (self.view.frame.size.height - 216.0);//求出键盘顶部与textfield底部大小的距离
    NSTimeInterval animationDuration = 0.30f;               
    [UIViewbeginAnimations:@"ResizeForKeyBoard"context:nil];               
    [UIViewsetAnimationDuration: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); //上推键盘操作,view大小始终没变            
        self.view.frame = rect;       
    }       
    [UIViewcommitAnimations];               
}


本文来源:http://www.bbyears.com/shoujikaifa/82608.html

热门标签

更多>>

本类排行