iOS进阶指南程序员iOS Developer

基础控件-->>UITextField详解

2016-09-20  本文已影响318人  MrBrave丶彬彬

继基础控件UIButton之后,期待的UITextField详细介绍-->>保证你有意外收获,如有问题欢迎指点。。


1、UITextField相关属性


#######效果

E5A98654-9F96-4EB1-8425-E9C7731EE9E2.png

#######代码

//初始化
    UITextField * textField = [[UITextField alloc]init];
    //位置
    textField.frame = CGRectMake(15, 100, [UIScreen mainScreen].bounds.size.width  - 30, 40);
    //背景
    textField.backgroundColor = [UIColor whiteColor];
    //外框类型
    [textField setBorderStyle:UITextBorderStyleRoundedRect];
    
    //设置默认的文字
    textField.placeholder = @"textField默认文字";
    //文本颜色 大小
    textField.textColor = [UIColor cyanColor];
    textField.font = [UIFont systemFontOfSize:14];
    //文本内容对对齐方式
    textField.textAlignment = NSTextAlignmentLeft;
    
    //修改默认文字的颜色 大小 KVC
    [textField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
    [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

    //安全 ** default is NO
     // textField.secureTextEntry = YES;
    
    //尾巴小X
    /** 
     typedef enum {
     UITextFieldViewModeNever, 从不出现
     UITextFieldViewModeWhileEditing, 编辑时出现
     UITextFieldViewModeUnlessEditing, 除了编辑外都出现
     UITextFieldViewModeAlways 一直出现
     } UITextFieldViewMode;
     
     */
    textField.clearButtonMode = UITextFieldViewModeAlways;
    //再次编辑清空
    textField.clearsOnBeginEditing = NO;
    //内容的垂直对齐方式  UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    
    //设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动
    textField.adjustsFontSizeToFitWidth = YES;
    
    //设置自动缩小显示的最小字体大小
    textField.minimumFontSize = 20;
    
    //设置键盘的样式
    /** 
     
     typedef enum {
     UIKeyboardTypeDefault,       默认键盘,支持所有字符
     UIKeyboardTypeASCIICapable,  支持ASCII的默认键盘
     UIKeyboardTypeNumbersAndPunctuation,  标准电话键盘,支持+*#字符
     UIKeyboardTypeURL,            URL键盘,支持.com按钮 只支持URL字符
     UIKeyboardTypeNumberPad,              数字键盘
     UIKeyboardTypePhonePad,     电话键盘
     UIKeyboardTypeNamePhonePad,   电话键盘,也支持输入人名
     UIKeyboardTypeEmailAddress,   用于输入电子 邮件地址的键盘
     UIKeyboardTypeDecimalPad,     数字键盘 有数字和小数点
     UIKeyboardTypeTwitter,        优化的键盘,方便输入@、#字符
     UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
     } UIKeyboardType;
     */
    //textField.keyboardType = UIKeyboardTypeNumberPad;
    
    
    //自动纠正 <没什么效果>
   /** 
    typedef  enum  {
    UITextAutocorrectionTypeDefault, //默认
    UITextAutocorrectionTypeNo, //不自动更正
    UITextAutocorrectionTypeYes, //自动更正
    }
    */
    textField . autocorrectionType  =  UITextAutocorrectionTypeYes ;
    
    
    
    //在 TextFiled左边加东西
    
    
    UIImageView  * imageView =[[UIImageView alloc]init];
    imageView.frame = CGRectMake(0, 0, 40, 40);
    imageView.image = [UIImage imageNamed:@"find_top_1"];
    textField.leftView = imageView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    
   //设置边框
    textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    textField.layer.borderWidth = 1;
    textField.layer.cornerRadius = 5;
    textField.clipsToBounds = YES;
    
    //代理
    textField.delegate = self;
    textField.tag =11;
    
    [self.view addSubview:textField];

2、UITextField相关代理


#######返回键 收回键盘

-(BOOL)textFieldShouldturn:(UITextField *)textField
{
    //返回一个BOOL值,指明是否允许在按下回车键时结束编辑
    //如果允许要调用resignFirstResponder 方法,这回导致结束编辑,而键盘会被收起       
    [textField resignFirstResponder];
    [textField resignFirstResponder];
    return YES;
}

#######指定是否循序文本字段开始编辑

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}

#######开始编辑时触发,文本字段将成为first responder

- (void)textFieldDidBeginEditing:(UITextField *)textField{
}

#######是否允许文本字段结束编辑,当编辑结束,文本字段会让出first responder

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    //要想在用户结束编辑时阻止文本字段消失,可以返回NO
    //这对一些文本字段必须始终保持活跃状态的程序很有用,比如即时消息
    return NO;
}

#######此方法很重要 限制字数等。。

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    //当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。
    //这对于想要加入撤销选项的应用程序特别有用
    //可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。
    //要防止文字被改变可以返回NO
    //这个方法的参数中有一个NSRange对象,指明了被改变文字的位置,建议修改的文本也在其中

   //限制字数 很简单 例子
    if (textField.tag == 11) {
        int count = 11;
        NSMutableString *newtxt = [NSMutableString stringWithString:textField.text];
        [newtxt replaceCharactersInRange:range withString:string];
        return ([newtxt length] <= count);
    }
    return YES;
}

#######是否允许根据用户请求清除内容

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    //可以设置在特定条件下才允许清除内容
    return YES;
}
上一篇 下一篇

猜你喜欢

热点阅读