现在的位置: 首页 > 综合 > 正文

UITextField 基本使用方法总结

2013年08月12日 ⁄ 综合 ⁄ 共 6362字 ⁄ 字号 评论关闭

1.UITextField的初始化和设置

 

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];

textField.borderStyle = UITextBorderStyleRoundedRect;

textField.font = [UIFont systemFontOfSize:15];

textField.placeholder = @"enter text";

textField.autocorrectionType = UITextAutocorrectionTypeNo;

textField.keyboardType = UIKeyboardTypeDefault;

textField.returnKeyType = UIReturnKeyDone;

textField.clearButtonMode = UITextFieldViewModeWhileEditing;

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    

textField.delegate = self;

[self.view addSubview:textField];

[textField release];

显示keyboard:

[textField becomeFirstResponder];

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    [textField becomeFirstResponder];

    return YES;

}

 

隐藏keyboard

[textField resignFirstResponder];

 

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    return YES;

}

****************************************************************************************************************************************************************************

新增加内容:

textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X 

xib中
  选中文本框后找到clear Button 后,选择 Appears while editing

****************************************************************************************************************************************************************************

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

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

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

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

  text.textColor = [UIColor redColor];

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

  text.textAlignment = UITextAlignmentLeft;

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

3.一次性关闭所有键盘 

  [ [ [ UIApplication sharedApplication ] keyWindow] endEdidting:YES ];  //关闭键盘

/*

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

CALayer *layer=[txtf1 layer];

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

[layer setMasksToBounds:YES];

//设置边框圆角的弧度

[layer setCornerRadius:5.0];

//设置边框线的宽

[layer setBorderWidth:1];

//设置边框线的颜色

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

*/

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

 
text.borderStyle = UITextBorderStyleRounded
Rect;

  typedef enum {

    UITextBorderStyleNone, 

    UITextBorderStyleLine,

    UITextBorderStyleBezel,

    UITextBorderStyleRoundedRect  

  } UITextBorderStyle;

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

  text.backgroundColor = [UIColor whiteColor];

5.设置背景图片   

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

6.设置选中背景图片

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

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

  text.placeholder = @"password";

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

  text.clearButtonMode = UITextFieldViewModeAlways;

  typedef enum {

    UITextFieldViewModeNever 从来不出现

    UITextFieldViewModeWhileEditing, 当编辑时候出现

    UITextFieldViewModeUnlessEditing,  除了编辑外都出现

    UITextFieldViewModeAlways   一直出现

  } UITextFieldViewMode;

9.输入内容为密码样式

  text.secureTextEntry = YES;

10.是否纠错

  text.autocorrectionType = UITextAutocorrectionTypeNo;

  typedef enum {

    UITextAutocorrectionTypeDefault, 默认

    UITextAutocorrectionTypeNo,   不自动纠错

    UITextAutocorrectionTypeYes,  自动纠错

  } UITextAutocorrectionType;

11.再次编辑时候清空

  text.clearsOnBeginEditing = YES;

12.设置键盘的样式

  text.keyboardType = UIKeyboardTypeNumberPad;

  typedef enum {

    UIKeyboardTypeDefault,       默认键盘,支持所有字符         

    UIKeyboardTypeASCIICapable,  支持ASCII的默认键盘

    UIKeyboardTypeNumbersAndPunctuation,  标准电话键盘,支持+*#字符

    UIKeyboardTypeURL,            URL键盘,支持.com按钮 只支持URL字符

    UIKeyboardTypeNumberPad,              数字键盘

    UIKeyboardTypePhonePad,     电话键盘

    UIKeyboardTypeNamePhonePad,   电话键盘,也支持输入人名

    UIKeyboardTypeEmailAddress,   用于输入电子 邮件地址的键盘     

    UIKeyboardTypeDecimalPad,     数字键盘 有数字和小数点

    UIKeyboardTypeTwitter,        优化的键盘,方便输入@#字符

    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, 

  } UIKeyboardType;

13.首字母是否大写

  text.autocapitalizationType = UITextAutocapitalizationTypeNone;

  typedef enum {

    UITextAutocapitalizationTypeNone, 不自动大写

    UITextAutocapitalizationTypeWords,  单词首字母大写

    UITextAutocapitalizationTypeSentences,  句子的首字母大写

    UITextAutocapitalizationTypeAllCharacters, 所有字母都大写

  } UITextAutocapitalizationType;

14.键盘外观

  textView.keyboardAppearance=UIKeyboardAppearanceDefault

  typedef enum {

    UIKeyboardAppearanceDefault 默认外观,浅灰色

    UIKeyboardAppearanceAlert     深灰 石墨色

  } UIReturnKeyType;

15.return键变成什么键

  text.returnKeyType =UIReturnKeyDone;

  typedef enum {

    UIReturnKeyDefault, 默认 灰色按钮,标有Return

    UIReturnKeyGo,      标有Go的蓝色按钮

    UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索

    UIReturnKeyJoin,标有Join的蓝色按钮

    UIReturnKeyNext,标有Next的蓝色按钮

    UIReturnKeyRoute,标有Route的蓝色按钮

    UIReturnKeySearch,标有Search的蓝色按钮

    UIReturnKeySend,标有Send的蓝色按钮

    UIReturnKeyYahoo,标有Yahoo的蓝色按钮

    UIReturnKeyYahoo,标有Yahoo的蓝色按钮

    UIReturnKeyEmergencyCall紧急呼叫按钮

  } UIReturnKeyType;

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

    text.delegate = self;

  [self.window addSubview:text];

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

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

    text.rightView=image;

    text.rightViewMode = UITextFieldViewModeAlways; 

  typedef enum {

    UITextFieldViewModeNever,

    UITextFieldViewModeWhileEditing,

    UITextFieldViewModeUnlessEditing,

    UITextFieldViewModeAlways

  } UITextFieldViewMode;

18.return键键盘往下收  becomeFirstResponder(要采用UITextFieldDelegate协议

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [text resignFirstResponder];    //主要是[receiver resignFirstResponder]在哪调用就能把receiver对应的键盘往下收

    return YES;

}

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

//限制输入的最大字数

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

抱歉!评论已关闭.