UITextFiled 使用细节

引言:


# 一. 常用属性

# 1. 文本显示属性:


textFiled.text 				// 获取文本内容
textFiled.textColor 		// 设置文本内容的颜色
textFiled.textAlignment 	// 设置文本对齐方式(水平方向)

textFiled.font 				// 设置文本字体
textField.font = [UIFont fontWithName:@“Helevetica-Bold” size: 20]; // 黑体加粗

textFiled.placeholder 		// 设置占位符(没有任何输入时,给出的提示字符串)

textFiled.autocapitalizationType // 设置首字母是否大写

textFiled.autocorrectionType //是否纠错
typedef enum {
	UITextAutocorrectionTypeDefault, //默认
	UITextAutocorrectionTypeNo, //不自动纠错
	UITextAutocorrectionTypeYes, //自动纠错
} UITextAutocorrectionType;

# 2. 文本输入属性:


secureTextEntry 			// 设置是否以密文方式输入
keyboardType 				// 弹出键盘的类型
clearOnBeginEditing 	// 是否开始输入的时候清空输入框内容
enabled			 		// 是否允许输入数据
returnKeyType 			// return按钮类型

# 3. 外观控制属性:


textFiled.borderStyle 			// 边框样式(枚举值)
textFiled.clearButtonMode 		// 清除按钮模式(枚举值)
typedef enum {
	UITextFieldViewModeNever, 				重不出现
	UITextFieldViewModeWhileEditing, 		编辑时出现
	UITextFieldViewModeUnlessEditing, 	除了编辑外都出现
	UITextFieldViewModeAlways 				一直出现
} UITextFieldViewMode;

textFiled.leftView 				// 输入框左视图
textFiled.leftViewMode 			// 左视图的显示模式
textFiled.rightView 			// 输入框右视图
textFiled.rightViewMode 		// 右视图的显示模式
textFiled.keyboardAppearance 	// 键盘外观设置

textFiled.returnKeyType // return键变成什么键
typedef enum {
	UIReturnKeyDefault, // 默认 灰色按钮,标有Return
	UIReturnKeyGo, // 标有Go的蓝色按钮
	UIReturnKeyGoogle, // 标有Google的蓝色按钮,用语搜索
	UIReturnKeyJoin, // 标有Join的蓝色按钮
	UIReturnKeyNext, // 标有Next的蓝色按钮
	UIReturnKeyRoute, // 标有Route的蓝色按钮
	UIReturnKeySearch, // 标有Search的蓝色按钮
	UIReturnKeySend, // 标有Send的蓝色按钮
	UIReturnKeyYahoo, // 标有Yahoo的蓝色按钮
	UIReturnKeyYahoo, // 标有Yahoo的蓝色按钮
	UIReturnKeyEmergencyCall, //紧急呼叫按钮
} UIReturnKeyType;

# 二. UITextFieldDelegate代理

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


//返回一个BOOL值,指定是否循序文本字段开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
	return YES;
}

# 2. 开始编辑时触发:


// 开始编辑时触发
- (void)textFieldDidBeginEditing:(UITextField *)textField{
	//开始编辑时触发,文本字段将成为first responder
}

# 3. 是否允许文本字段结束编辑:


//当编辑结束,文本字段会让出first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
	//返回BOOL值,指定是否允许文本字段结束编辑,当编辑结束,文本字段会让出first responder

	//要想在用户结束编辑时阻止文本字段消失,可以返回NO

	//这对一些文本字段必须始终保持活跃状态的程序很有用,比如即时消息
	return NO;
}

# 4. 键盘编辑时 监控键盘内容变化:


// 键盘编辑时 监控键盘内容变化
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

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

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


- (BOOL)textFieldShouldClear:(UITextField *)textField{
	//返回一个BOOL值指明是否允许根据用户请求清除内容

	//可以设置在特定条件下才允许清除内容
	return YES;
}

# 6. 是否允许在按下回车键时结束编辑:


-(BOOL)textFieldShouldReturn:(UITextField *)textField{

	//返回一个BOOL值,指明是否允许在按下回车键时结束编辑
	//如果允许要调用resignFirstResponder 方法,这回导致结束编辑,而键盘会被收起[textField resignFirstResponder];
	//查一下resign这个单词的意思就明白这个方法了

	return YES;
}

# 三. 键盘回收

核心: 取消第一响应者 [textField resignFirstResponder]; 成为第一响应者 [next becomeFirstResponder];


- (BOOL)textFieldShouldReturn:(UITextField *)textField{

	if(textField.tag == 101){

		//取消第一响应者(正在和用户进行交互的 称为响应者,同一时间内只能有一个响应者)
		[textField resignFirstResponder];
		//取出下一个密码框
		UITextField *next = (UITextField *)[self.window viewWithTag:102];//强制转换
		//让下一个输入框成为第一响应者
		[next becomeFirstResponder];
	}
	
	[textField resignFirstResponder];
	return YES;
}

# 四. 限制输入一定长度的字符


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

	//string就是此时输入的那个字符 textField就是此时正在输入的那个输入框 返回YES就是可以改变输入框的值 NO相反

	if ([string isEqualToString:@"\n"]) {
		//按会车可以改变
		return YES;
	}

	//得到输入框的内容
	NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];

	if (self.myTextField == textField) {//判断是否时我们想要限定的那个输入框

		if ([toBeString length] > 20) { //如果输入框内容大于20则弹出警告
			textField.text = [toBeString substringToIndex:20];
			UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil message:@"超过最大字数不能输入了" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] autorelease];

			[alert show];

			return NO;
		}
	}
	return YES;
}

# 五. 限制只能输入特定的字符


-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSCharacterSet *cs;

	cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];

	//按cs分离出数组,数组按@""分离出字符串
	NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];

	BOOL canChange = [string isEqualToString:filtered];
	return canChange;
}

# 六. 设置 placeholder 相关属性


UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
textField.backgroundColor = [UIColor greenColor];

// 占位文字
NSString *placeholderText = @"占位文字";
NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:placeholderText];

// 占位文字颜色
[placeholder addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, placeholderText.length)];

// 占位文字字号
[placeholder addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:NSMakeRange(0, placeholderText.length)];

// 占位文字 水平居中
NSMutableParagraphStyle *style = [textField.defaultTextAttributes[NSParagraphStyleAttributeName] mutableCopy];

CGFloat width = [placeholderText boundingRectWithSize:(CGSizeMake(MAXFLOAT, [UIFont systemFontOfSize:16.0].lineHeight)) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16.0]} context:nil].size.width;

style.firstLineHeadIndent = (textField.frame.size.width - textWidth )*0.5;
[placeholder addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, placeholderText.length)];


// 占位文字 垂直居中
NSMutableParagraphStyle *style = [textField.defaultTextAttributes[NSParagraphStyleAttributeName] mutableCopy];
style.minimumLineHeight = textField.font.lineHeight - (textField.font.lineHeight - [UIFont systemFontOfSize:16.0].lineHeight) / 2.0;
[placeholder addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, placeholderText.length)];

textField.attributedPlaceholder = placeholder;
[view addSubview:textField];