UITextField (文本输入框基本设置和代理)


其它完整的属性解析网址:http://blog.csdn.net/mr_rog/article/details/40896759


监听textFiled最终输入内容

[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];  

-(void)textFieldDidChange :(UITextField *)theTextField{  
    NSLog( @"text changed: %@", theTextField.text);  
}



无文字时,Return为灰色不点 

textField.enablesReturnKeyAutomatically = YES; //这里设置为无文字就灰色不可点


- (void)viewDidLoad
{
    [super viewDidLoad];

    //初始化
    UITextField *textField=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    textField.backgroundColor=[UIColor whiteColor];
    textField.delegate = self;
    [self.view addSubview:textField];

    
    //描边、圆角 (A 和 B方法有冲突只能选其一,A方法重绘 B方法系统预设效果)
    //___A
    textField.layer.borderColor=[UIColor redColor].CGColor;
    textField.layer.borderWidth=1;
    textField.layer.cornerRadius = 5;
    //___B
//    textField.borderStyle = UITextBorderStyleRoundedRect;
    
    
    //当有输入内容时显示清空 “X”
    textField.clearButtonMode=UITextFieldViewModeWhileEditing;
    
    
    /**
     *  UIKeyboardType 键盘类型 (对应图文可以参考 http://my.oschina.net/p31415/blog/297045)
     *  开发常用类型
     *  UIKeyboardTypeDefault  默认全可切换的全类型键盘  (如果不修改可以不用设置keyboardType)
     *  UIKeyboardTypeNumberPad 纯数字键盘 (一般在价格框、验证码框限制为数字输入)
     *  UIKeyboardTypeASCIICapable 字母+数字+语言
     */
    textField.keyboardType=UIKeyboardTypeDefault;
    
    
    /**
     *  UIReturnKeyType 键盘return键样式
     *  enum 
     UIReturnKeyDefault,        "return"
     UIReturnKeyGo,             "GO"
     UIReturnKeyGoogle,         "Search"
     UIReturnKeyJoin,           "Join"
     UIReturnKeyNext,           "Next"
     UIReturnKeyRoute,          "Route"
     UIReturnKeySearch,         "Search"
     UIReturnKeySend,           "Send"
     UIReturnKeyYahoo,          "Search"
     UIReturnKeyDone,           "Done"
     UIReturnKeyEmergencyCall,  "EmergencyCall"
     *
     */
    textField.returnKeyType = UIReturnKeyDone;
    
    
    //灰色提示文字
    textField.placeholder = @"请输入内容";
    //修改提示文字颜色
    [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];

    /**
     *   是否跟随输入内容变为 圈圈 ****
     *   if Yes keyboardType just show char and number
     *
     */
//    textField.secureTextEntry=YES;

    
    /**
     *  UITextFieldViewMode 光标前边的view
     *  用法:1、可以用一个空白view让光标初始位置根据view宽度靠右
     *      2、设置相应图案icon
     */
    textField.leftViewMode=UITextFieldViewModeAlways;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 10)];
    view.backgroundColor = [UIColor blueColor];
    textField.leftView = view;
    
    //背景图片
    UIImage *image = [UIImage imageNamed:@"monthselect"];
    textField.background = image;

    //disabledBackground  边缘图案 基本没用上,看不出效果,有用过的可以分享一下
//    textField.disabledBackground = image;
    
    
    /**
     *  @property(nonatomic) BOOL allowsEditingTextAttributes
        是否允许更改字符属性字典
     */
    textField.allowsEditingTextAttributes = YES;
//    textField.attributedText = 参照UILabel设置多种颜色字体
    
    
    //第一响应者,键盘弹出
    [textField becomeFirstResponder];
    
    //放弃第一响应,键盘收起
    [textField resignFirstResponder];
    
    
    /**
     *   @property (readwrite, retain) UIView *inputAccessoryView;  自定义键盘顶部工具栏
     */
    UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 50)];
    accessoryView.backgroundColor = [UIColor blueColor];
    textField.inputAccessoryView=accessoryView;
    
    
    //覆盖键盘区域视图,可以根据需求在键盘出现后点击切换 inputView
    //@property (readwrite, retain) UIView *inputView;
    
}


#pragma mark - delegate 回调顺序从上到下

//1.点击输入框准备编辑,调用两次
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    //1-->2-->1
    return YES;
}

//2.完成准备编辑动作 调用一次,键盘弹出
-(void)textFieldDidBeginEditing:(UITextField *)textField
{

    
    
}


//3.键盘点击跟踪 每点一下 string记录到当前 一笔一画、一个字母,和最终选择的文字
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //"一"  "a"
    return YES;
}


//4.点击输入框以外空白处,准备结束输入动作
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return YES;
}


//5.结束编辑,键盘收起
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    
}




//*点击清除文字按钮事件
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
    return YES;
}


//*点击return回调
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return YES;
}


收起键盘四种方法

    1.[textFiled resignFirstResponder];
    2.[self.view endEditing:YES];
    3.[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    4.[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

//取消键盘输入文字预测、提示

textField.autocorrectionType =UITextAutocorrectionTypeNo;




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值