您的位置:首页 > 产品设计 > UI/UE

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

2015-11-05 17:34 465 查看
其它完整的属性解析网址: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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: