UITextField 基本属性使用

//设置文本框 透明度
    tf.alpha = 1;
    //设置文本颜色
    tf.textColor = [UIColor orangeColor];
    //设置文本文字 格式
    tf.font = [UIFont systemFontOfSize:20];
    /*设置文本对齐方式
     NSTextAlignmentLeft 左
     NSTextAlignmentRight 右
     NSTextAlignmentCenter 中
    */
    tf.textAlignment = NSTextAlignmentCenter;
    /* 设置文本框 边框格式
     UITextBorderStyleNone 默认无
     
     UITextBorderStyleLine 线边框
     
     UITextBorderStyleBezel 阴影效果边框
     
     UITextBorderStyleRoundedRect 圆边效果
     */
    tf.borderStyle = UITextBorderStyleBezel;
    //文本框 格式化文字输入
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
    
    NSAttributedString * attr = [[NSAttributedString alloc]initWithString:@"cccccccc" attributes:dic];
//    tf.attributedText = attr;
    //defaultTextAttributes 设置文本框的 默认字体属性
    //输入的字成为  点用于密码框
    tf.secureTextEntry = NO;
    
    //首字母是否大写
    /*
     UITextAutocapitalizationTypeNone, 不自动大写
     
     UITextAutocapitalizationTypeWords,  单词首字母大写
     
     UITextAutocapitalizationTypeSentences,  句子的首字母大写
     
     UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
     */
    tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    //内容垂直对齐方式  继承自uicontrol中contentVerticalAlignment
    tf.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    //设置 提示字 文本框为空的时候显示 默认颜色为灰色
    tf.placeholder = @"ccasdfjkdlajbasdf";
    
    //设置 格式化提示字体
    tf.attributedPlaceholder = attr;
    
    //设置是否在开始编辑的时候清空 文本框 默认为NO
    tf.clearsOnBeginEditing = YES;
    
    //设置字体大小是否随宽度自适应 ,跟设置最小字体一起使用
    tf.adjustsFontSizeToFitWidth = YES;
    
     //设置最小字体
    tf.minimumFontSize = 10.0;
    //设置背景图片 设置背景图片时图片会被拉伸  设置文本框边框会影响 图片显示
    tf.background = [UIImage imageNamed:@"332.jpg"];
    //设置文本框禁用时图片 边框不会影响 
//    tf.enabled = NO;
    tf.disabledBackground = [UIImage imageNamed:@"223.jpg"];
    
    //tf.editing 是否正在编辑
   [self setValue:[NSNumber numberWithBool:YES] forKeyPath:@"tf.adjustsFontSizeToFitWidth"];
    NSLog(@"%i",tf.isEnabled);
    //allowsEditingTextAttributes 是否允许更改文本属性字典 默认为no
    tf.allowsEditingTextAttributes = YES;
    //设置字符属性字典 值修改了提示字的 格式  输入的文字怎没有变化
    tf.typingAttributes = dic;
    
    //设置文本框清空按钮 显示状态
    /*
     UITextFieldViewModeNever 不显示
     
     UITextFieldViewModeWhileEditing 当修改时 常用
     
     UITextFieldViewModeUnlessEditing 非编辑状态显示
     
     UITextFieldViewModeAlways 一直显示
     */
    tf.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    //添加文本框左右识图 默认显示状态为UITextFieldViewModeNever
    tf.leftView = [UIView new];
    tf.rightView = [UIView new];
    //
    /* 设置文本框左右视图 显示状态意思与清空按钮状态一样
     tf.leftViewMode 修改左视图显示状态
     tf.rightViewMode 修改右视图显示状态
     
     UITextFieldViewModeNever 从不
     
     UITextFieldViewModeWhileEditing 修改时
     
     UITextFieldViewModeUnlessEditing 非修改时
     
     UITextFieldViewModeAlways 一直
     */
    //使用后键盘不出现
    //设置当文本框成为第一响应者是弹出的视图 底部出现宽度默认为屏幕宽度 高度自定义 x,y,width 设置了都不管用
    UIView * c = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 70)];
    c.backgroundColor = [UIColor greenColor];
    tf.inputView = c;
    //设置文本框成为第一响应者弹出的辅助视图 出现在inputview上 于in普通View一样 只有高度发生了变化
    UIView *fz = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 30, 50)];
    fz.backgroundColor = [UIColor redColor];
    tf.inputAccessoryView = fz;
    //设置文本框 再次输入文本时(重新获取焦点)不允许插入文字
    tf.clearsOnInsertion = YES;
//键盘类型
    /*
     UIKeyboardTypeDefault,       默认键盘,支持所有字符
     
     UIKeyboardTypeASCIICapable,  支持ASCII的默认键盘
     
     UIKeyboardTypeNumbersAndPunctuation,  标准电话键盘,支持+*#字符
     
     UIKeyboardTypeURL,            URL键盘,支持.com按钮 只支持URL字符
     
     UIKeyboardTypeNumberPad,              数字键盘
     
     UIKeyboardTypePhonePad,     电话键盘
     
     UIKeyboardTypeNamePhonePad,   电话键盘,也支持输入人名
     
     UIKeyboardTypeEmailAddress,   用于输入电子 邮件地址的键盘
     
     UIKeyboardTypeDecimalPad,     数字键盘 有数字和小数点
     
     UIKeyboardTypeTwitter,        优化的键盘,方便输入@、#字符
     
     UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
     */
    tf.keyboardType = UIKeyboardTypeTwitter;
    
    //设置键盘颜色
    /*
     UIKeyboardAppearanceDefault,  浅灰
     
     UIKeyboardAppearanceDark  黑
     
     UIKeyboardAppearanceLight 高亮
     
     UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark, 深灰
     */
    tf.keyboardAppearance = UIKeyboardAppearanceLight;
    
    //设置键盘return键 成什么键
    /*
     UIReturnKeyDefault,
     UIReturnKeyGo,
     UIReturnKeyGoogle,
     UIReturnKeyJoin,
     UIReturnKeyNext,
     UIReturnKeyRoute,
     UIReturnKeySearch,
     UIReturnKeySend,
     UIReturnKeyYahoo,
     UIReturnKeyDone,
     UIReturnKeyEmergencyCall 紧急呼叫
     UIReturnKeyContinue
     */
    tf.returnKeyType = UIReturnKeyEmergencyCall;
    
}
#pragma MARK: -  触屏失去焦点
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    //注销第一响应者  收回键盘
    [tf endEditing:YES];
//    [tf resignFirstResponder];
}

 下面是代理方法的实现应用

头部UITextFieldDelegate协议 <UITextFieldDelegate> 
遵守 协议
遵守协议 tf.delegate = self;
#pragma MARK: - UITextFiledDelegate 实现
//点击输入框输入该方法   返回yes进入编辑状态, no则不能编辑 每次获取焦点后触发
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    tf.text = @"我决定能不能让你编辑我~~~~~";
    return YES;
}
//开始编辑时触发方法
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"我不我就不,我要编辑了~~~~");
}
//将要结束编辑时调用方法, 返回YES结束 返回NO不能
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    NSLog(@"将要结束编辑 咯~~~~~~   我返回yes就结束了,返回no 还得编瞎话~~");
    return YES;
}
//结束编辑调用的方法
- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"我现在真的结束了!!!!!");
}
//输入字符时调用的方法 返回yes允许输入 返回no不允许输入
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSLog(@"我输入了字符~~~输入咯哦~~");
    return YES;
}
//点击清理按钮时触发的方法 返回yes清理  no 不清理
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    NSLog(@"就不清,就不清~~~~怎么样。。。。");
    return YES;
}
//单击return 时触发的方法 yes执行 no不执行
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"yes");
    return YES;
}

 

今天倒腾了一下午整理了 一下textfield 的基本使用属性 UITextField联系到好多键盘的设置 后面还有关于重绘UITextField的之后再做补充

注意一些属性之间的相互冲突 今天碰到一个问题 当弄出屏幕键盘后我的  键盘打字上不去只能用屏幕键盘才能输入 , 当跳出inputview后 键盘就不在出现 这个东西不太明白有没有大神说一下

posted on 2016-03-31 20:41  GG.Bong  阅读(341)  评论(2编辑  收藏  举报