zoukankan      html  css  js  c++  java
  • iOS阶段学习第29天笔记(UITextField的介绍)

    iOS学习(UI)知识点整理

    一、关于UITextField的介绍 

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

    2)UITextField  初始化实例代码:

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

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

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

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

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

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

    1  textField.layer.cornerRadius = 4.0f;

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

    1 textField.layer.borderWidth = 1;

     

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

    1  textField.layer.borderColor = [UIColor darkGrayColor].CGColor;

     
    8)background 设置文本框背景图片 例如: 

    1 UIImage *image = [UIImage imageNamed:@"btnEmojBtn"];
    2  textField.background = image;

    9)设置文本框默认显示文字(默认灰色字体点击键盘输入时文字消失)例如: 

    方法一:placeholder 

    1  textField.placeholder = @"用户名";

    方法二: NSMutableAttributedString 

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

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

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

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

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

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

    1 textField.returnKeyType = UIReturnKeyDone;

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

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

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

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

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

    1 [textField resignFirstResponder];

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

    1  textField .secureTextEntry = YES;

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

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

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

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

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

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

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

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

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

    1   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;     
  • 相关阅读:
    面向对象基础
    VmWare下安装CentOS6图文安装教程
    设计模式培训之一:为什么要用单例模式?
    CentOS5.4下安装和配置Apache、PHP、MySql、PHPMyAdmin
    WEB架构师成长系列索引
    WEB架构师成长之路之三架构师都要懂哪些知识
    设计模式培训之三:抽象工厂
    IOS6屏幕自动旋转设置测试
    设计模式培训之二:简单工厂、工厂方法
    QT和Oracle连接的oci驱动的编译
  • 原文地址:https://www.cnblogs.com/ChinaKingKong/p/4690581.html
Copyright © 2011-2022 走看看