UITextField 基本使用方法总结

1.初始化textfield并设置位置及大小

UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];

2.设置输入框内容的字体样式和大小,字体颜色,对齐方式

<wbr>text.font = [UIFont fontWithName:@"Arial" size:20.0f];</wbr>

text.textColor = [UIColor redColor];

text.text = @"输入框默认文字";

text.textAlignment = UITextAlignmentLeft;

text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//垂直居中

/*

//设置圆角边框方法二,该方法可以设置任意控件圆角边框,可以定义圆角的弧度、颜色、边框等

CALayer *layer=[txtf1 layer];

//是否设置边框以及是否可见

[layer setMasksToBounds:YES];

//设置边框圆角的弧度

[layer setCornerRadius:5.0];

//设置边框线的宽

[layer setBorderWidth:1];

//设置边框线的颜色

[layer setBorderColor:[[UIColor blackColor] CGColor]];

*/


3.设置边框样式,只有设置了才会显示边框样式

text.borderStyle = UITextBorderStyleRounded<wbr style="color:rgb(50,62,50); font-family:Menlo; font-size:14px; text-align:left"><span style="color:rgb(50,62,50); font-family:Menlo; font-size:14px; text-align:left">Rect;</span><br></wbr>

typedef enum {

<wbr><wbr>UITextBorderStyleNone,<wbr></wbr></wbr></wbr>

<wbr><wbr>UITextBorderStyleLine,</wbr></wbr>

<wbr><wbr>UITextBorderStyleBezel,</wbr></wbr>

<wbr><wbr>UITextBorderStyleRounded<wbr>Rect <wbr></wbr></wbr></wbr></wbr>

<wbr>} UITextBorderStyle;</wbr>

4.设置输入框的背景颜色,此时设置为白色<wbr></wbr>如果使用了自定义的背景图片边框会被忽略掉

text.backgroundColor = [UIColor whiteColor];

5.设置背景图片

text.background = [UIImage imageNamed:@"dd.png"];

6.设置选中背景图片

text.disabledBackground = [UIImage imageNamed:@"cc.png"];

7.当输入框没有内容时,水印提示<wbr></wbr>提示内容为password

<wbr>text.placeholder = @"password";</wbr>

8.输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容

<wbr>text.clearButtonMode = UITextFieldViewModeAlway<wbr>s;</wbr></wbr>

typedef enum {

<wbr><wbr>UITextFieldViewModeNever<wbr>,<wbr>从来<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">不出现</span></wbr></wbr></wbr></wbr>

<wbr><wbr>UITextFieldViewModeWhile<wbr>Editing,<wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">当编辑时候出现</span></wbr></wbr></wbr></wbr>

<wbr><wbr>UITextFieldViewModeUnles<wbr>sEditing,<wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">除了编辑外都出现</span></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>UITextFieldViewModeAlway<wbr>s <wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">一直出现</span></wbr></wbr></wbr></wbr></wbr>

} UITextFieldViewMode;

9.输入内容为密码样式

text.secureTextEntry = YES;

10.是否纠错

text.autocorrectionType = UITextAutocorrectionType<wbr style="color:rgb(50,62,50); font-family:Menlo; font-size:14px; text-align:left"><span style="color:rgb(50,62,50); font-family:Menlo; font-size:14px; text-align:left">No;</span></wbr>

typedef enum {

<wbr><wbr>UITextAutocorrectionType<wbr>Default,<wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">默认</span></wbr></wbr></wbr></wbr>

<wbr><wbr>UITextAutocorrectionType<wbr>No, <wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">不自动纠错</span></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>UITextAutocorrectionType<wbr>Yes,<wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">自动纠错</span></wbr></wbr></wbr></wbr></wbr>

} UITextAutocorrectionType<wbr>;</wbr>

11.再次编辑时候清空

text.clearsOnBeginEditing = YES;

12.设置键盘的样式

<wbr>text.keyboardType = UIKeyboardTypeNumberPad;</wbr>

typedef enum {

<wbr><wbr>UIKeyboardTypeDefault, <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">默认键盘,支持所有字符</span><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>UIKeyboardTypeASCIICapab<wbr>le,<wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">支持</span>ASCII<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的默认键盘</span></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>UIKeyboardTypeNumbersAnd<wbr>Punctuation,<wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">标准电话键盘,支持+*#字符</span></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>UIKeyboardTypeURL,<wbr><wbr><wbr><wbr><wbr><wbr>URL<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">键盘,支持</span>.com<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">按钮</span><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">只支持</span>URL<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">字符</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

UIKeyboardTypeNumberPad,<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">数字键盘</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

UIKeyboardTypePhonePad,<wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">电话键盘</span></wbr></wbr></wbr></wbr>

<wbr><wbr>UIKeyboardTypeNamePhoneP<wbr>ad, <wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">电话键盘,也支持输入人名</span></wbr></wbr></wbr></wbr></wbr>

UIKeyboardTypeEmailAddre<wbr>ss, <wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">用于输入电子</span><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">邮件地址的键盘</span><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

UIKeyboardTypeDecimalPad<wbr>, <wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">数字键盘</span><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">有数字和小数点</span></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>UIKeyboardTypeTwitter,<wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">优化的键盘,方便输入</span>@<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">、</span>#<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">字符</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapab<wbr>le,<wbr></wbr></wbr></wbr></wbr>

} UIKeyboardType;

13.首字母是否大写

<wbr>text.autocapitalizationType = UITextAutocapitalization<wbr>TypeNone;</wbr></wbr>

typedef enum {

<wbr><wbr>UITextAutocapitalization<wbr>TypeNone,<wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">不自动大写</span></wbr></wbr></wbr></wbr>

<wbr><wbr>UITextAutocapitalization<wbr>TypeWords,<wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">单词首字母大写</span></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>UITextAutocapitalization<wbr>TypeSentences,<wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">句子的首字母大写</span></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>UITextAutocapitalization<wbr>TypeAllCharacters,<wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">所有字母都大写</span></wbr></wbr></wbr></wbr>

} UITextAutocapitalization<wbr>Type;</wbr>

14.键盘外观

textView.keyboardAppearance=UIKeyboardAppearanceDefa<wbr>ult<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">;</span></wbr>

typedef enum {

UIKeyboardAppearanceDefa<wbr>ult<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">,</span><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">默认外观,浅灰色</span></wbr></wbr>

UIKeyboardAppearanceAler<wbr>t<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">,</span><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">深灰</span><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">石墨色</span></wbr></wbr></wbr></wbr></wbr></wbr>

} UIReturnKeyType;

15.return键变成什么键

<wbr>text.returnKeyType =UIReturnKeyDone;</wbr>

typedef enum {

<wbr><wbr>UIReturnKeyDefault,<wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">默认</span><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">灰色按钮,标有</span>Return</wbr></wbr></wbr></wbr>

<wbr><wbr>UIReturnKeyGo,<wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">标有</span>Go<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的蓝色按钮</span></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>UIReturnKeyGoogle,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">标有</span>Google<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的蓝色按钮,用语搜索</span></wbr></wbr>

<wbr><wbr>UIReturnKeyJoin,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">标有</span>Join<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的蓝色按钮</span></wbr></wbr>

<wbr><wbr>UIReturnKeyNext,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">标有</span>Next<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的蓝色按钮</span></wbr></wbr>

<wbr><wbr>UIReturnKeyRoute,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">标有</span>Route<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的蓝色按钮</span></wbr></wbr>

<wbr><wbr>UIReturnKeySearch,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">标有</span>Search<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的蓝色按钮</span></wbr></wbr>

<wbr><wbr>UIReturnKeySend,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">标有</span>Send<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的蓝色按钮</span></wbr></wbr>

<wbr><wbr>UIReturnKeyYahoo,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">标有</span>Yahoo<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的蓝色按钮</span></wbr></wbr>

<wbr><wbr>UIReturnKeyYahoo,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">标有</span>Yahoo<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的蓝色按钮</span></wbr></wbr>

<wbr><wbr>UIReturnKeyEmergencyCall<wbr>,<wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">紧急呼叫按钮</span></wbr></wbr></wbr></wbr>

} UIReturnKeyType;

16.设置代理, textfield 加到视图中

text.delegate = self;

[self.window addSubview:text];

17.最右侧加图片左侧类似)

<wbr><wbr>UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];</wbr></wbr>

<wbr><wbr>text.rightView=image;</wbr></wbr>

<wbr><wbr>text.rightViewMode = UITextFieldViewModeAlway<wbr>s;</wbr></wbr></wbr>

typedef enum {

<wbr><wbr>UITextFieldViewModeNever<wbr>,</wbr></wbr></wbr>

<wbr><wbr>UITextFieldViewModeWhile<wbr>Editing,</wbr></wbr></wbr>

<wbr><wbr>UITextFieldViewModeUnles<wbr>sEditing,</wbr></wbr></wbr>

<wbr><wbr>UITextFieldViewModeAlway<wbr>s</wbr></wbr></wbr>

} UITextFieldViewMode;

18.return键键盘往下收<wbr style="color:rgb(220,32,3); font-family:Menlo; font-size:14px; text-align:left"><span style="color:rgb(220,32,3); font-family:Menlo; font-size:14px; text-align:left">becomeFirstResponder(<span style="word-wrap:normal; word-break:normal; color:rgb(220,32,3); font-size:14px; text-align:left; padding:0px; margin:0px; font-family:'Heiti sC Light'">要采用</span><span style="color:rgb(220,32,3); font-family:Menlo; font-size:14px; text-align:left">UITextFieldDelegate</span><span style="word-wrap:normal; word-break:normal; color:rgb(220,32,3); font-size:14px; text-align:left; padding:0px; margin:0px; font-family:'Heiti sC Light'">协议</span>)</span></wbr>

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

<wbr><wbr>[text resignFirstResponder];<wbr><wbr>//<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">主要是</span>[receiver resignFirstResponder]<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">在哪调用就能把</span>receiver<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">对应的键盘往下收</span></wbr></wbr></wbr></wbr>

<wbr><wbr>return YES;</wbr></wbr>

}

//补充点新内容 (从网上看到的)

//限制输入的最大字数

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

{

if ([string isEqualToString:@"\n"]){

return YES;

}

NSString * aString = [textField.text stringByReplacingCharactersInRange:range withString:string];

if (self.searchTextField == textField)

{

if ([aString length] > 5) {

textField.text = [aString substringToIndex:5];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil

message:@"超过最大字数不能输入了"

delegate:nil

cancelButtonTitle:@"Ok"

otherButtonTitles:nil, nil];

[alert show];

[alert release];

return NO;

}

}

return YES;


}

一下补充内容来源于网上:

重写绘制行为

除了UITextField对象的风格选项,你还可以定制化UITextField对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围。以下方法都可以重写。

–textRectForBounds:   //重写来重置文字区域

–drawTextInRect:   //改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super.

–placeholderRectForBounds:  //重写来重置占位符区域

–drawPlaceholderInRect:  //重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super.

–borderRectForBounds:  //重写来重置边缘区域

–editingRectForBounds:  //重写来重置编辑区域

–clearButtonRectForBounds:  //重写来重置clearButton位置,改变size可能导致button的图片失真

–leftViewRectForBounds:

–rightViewRectForBounds:

委托方法

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

//返回一个BOOL值,指定是否循序文本字段开始编辑

returnYES;

}

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

//开始编辑时触发,文本字段将成为firstresponder

}

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

//返回BOOL值,指定是否允许文本字段结束编辑,当编辑结束,文本字段会让出firstresponder

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

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

returnNO;

}

-(BOOL)textField:(UITextField*)textFieldshouldChangeCharactersInRange:(NSRange)rangereplacementString:(NSString*)string{

//当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。

//这对于想要加入撤销选项的应用程序特别有用

//可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。

//要防止文字被改变可以返回NO

//这个方法的参数中有一个NSRange对象,指明了被改变文字的位置,建议修改的文本也在其中

returnYES;

}

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

//返回一个BOOL值指明是否允许根据用户请求清除内容

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

returnYES;

}

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

//返回一个BOOL值,指明是否允许在按下回车键时结束编辑

//如果允许要调用resignFirstResponder方法,这回导致结束编辑,而键盘会被收起[textFieldresignFirstResponder];

//查一下resign这个单词的意思就明白这个方法了

returnYES;

}

通知

UITextField派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用。除了UIControl类的标准事件,你还可以使用下列UITextField类特有的事件

UITextFieldTextDidBeginEditingNotification

UITextFieldTextDidChangeNotification

UITextFieldTextDidEndEditingNotification

当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。

因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知

UIKeyboardWillShowNotification //键盘显示之前发送

UIKeyboardDidShowNotification //键盘显示之后发送

UIKeyboardWillHideNotification //键盘隐藏之前发送

UIKeyboardDidHideNotification //键盘隐藏之后发送

1Text:设置文本框的默认文本。

2Placeholder可以在文本框中显示灰色的字,用于提示用户应该在这个文本框输入什么内容。当这个文本框中输入了数据时,用于提示的灰色的字将会自动消失。

3、Background:

4Disabled若选中此项,用户将不能更改文本框内容。

5、接下来是三个按钮,用来设置对齐方式。

6、Border Style:选择边界风格。

7Clear Button这是一个下拉菜单,你可以选择清除按钮什么时候出现,所谓清除按钮就是出一个现在文本框右边的小X,你可以有以下选择:

7.1 Never appears:从不出现

7.2 Appears while editing:编辑时出现

7.3 Appears unless editing:

7.4 Is always visible:总是可见

8Clear when editing begins若选中此项,则当开始编辑这个文本框时,文本框中之前的内容会被清除掉。比如,你现在这个文本框A中输入了"What",之后去编辑文本框B,若再回来编辑文本框A,则其中的"What"会被立即清除。

9Text Color设置文本框中文本的颜色。

10Font设置文本的字体与字号。

11Min Font Size设置文本框可以显示的最小字体(不过我感觉没什么用)

12Adjust To Fit指定当文本框尺寸减小时,文本框中的文本是否也要缩小。选择它,可以使得全部文本都可见,即使文本很长。但是这个选项要跟Min Font Size配合使用,文本再缩小,也不会小于设定的Min Font Size

接下来的部分用于设置键盘如何显示。

13、Captitalization:设置大写。下拉菜单中有四个选项:

13.1 None:不设置大写

13.2 Words每个单词首字母大写,这里的单词指的是以空格分开的字符串

13.3 Sentances每个句子的第一个字母大写,这里的句子是以句号加空格分开的字符串

13.4 All Characters:所以字母大写

14、Correction:检查拼写,默认是YES。

15Keyboard选择键盘类型,比如全数字、字母和数字等。

16、Appearance:

17、Return Key:选择返回键,可以选择Search、Return、Done等。

18Auto-enable Return Key如选择此项,则只有至少在文本框输入一个字符后键盘的返回键才有效。

19Secure当你的文本框用作密码输入框时,可以选择这个选项,此时,字符显示为星号。

1.Alignment Horizontal水平对齐方式

2.Alignment Vertical垂直对齐方式

3.用于返回一个BOOL值输入框是否Selected(选中) Enabled(可用) Highlighted(高亮)

限制只能输入特定的字符

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

NSCharacterSet *cs;

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

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

BOOL canChange = [string isEqualToString:filtered];

return canChange;

}

上面那个NUMBERS是一个宏,可以在文件顶部定义:

#define NUMBERS @”0123456789\n”(这个代表可以输入数字和换行,请注意这个\n,如果不写这个,Done按键将不会触发,如果用在SearchBar中,将会不触发Search事件,因为你自己限制不让输入\n,好惨,我在项目中才发现的。)

所以,如果你要限制输入英文和数字的话,就可以把这个定义为:

#define kAlphaNum @”ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789″。

当然,你还可以在以上方法return之前,做一提示的,比如提示用户只能输入数字之类的。如果你觉得有需要的话。

限制只能输入一定长度的字符

- (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;

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值