iOS学习(4)——UITextField

来源:互联网 发布:软件企业认定多久 编辑:程序博客网 时间:2024/06/11 21:49
- (void)viewDidLoad {    [super viewDidLoad];    //创建一个text    UITextField *textView=[[UITextField alloc]init];    //设置text的frame    textView.frame=CGRectMake(0, 0, 0, 0);    //设置text的边框样式    textView.borderStyle=UITextBorderStyleRoundedRect;//    typedef NS_ENUM(NSInteger, UITextBorderStyle) {//        UITextBorderStyleNone,//        UITextBorderStyleLine,//        UITextBorderStyleBezel,//        UITextBorderStyleRoundedRect//    };    //设置text的背景颜色    textView.backgroundColor=[UIColor whiteColor];    //设置text的北京图片    textView.background=[UIImage imageNamed:@""];    //设置text的提示文字    textView.placeholder=@"请输入内容";    //设置text的文字    textView.text=@"text";    //设置text的文字大小    textView.font=[UIFont systemFontOfSize:14];    //设置text的文字颜色    textView.textColor=[UIColor blackColor];    //设置text的文字布局    textView.textAlignment=NSTextAlignmentRight;    //设置text的删除按钮,在什么时候显示,用于一次性删除输入框中的内容    textView.clearButtonMode = UITextFieldViewModeAlways;//    typedef enum {//        UITextFieldViewModeNever, 重不出现//        UITextFieldViewModeWhileEditing, 编辑时出现//        UITextFieldViewModeUnlessEditing, 除了编辑外都出现//        UITextFieldViewModeAlways  一直出现//    } UITextFieldViewMode;    //设置text的密码输入    textView.secureTextEntry = YES;    //设置text是否纠错    textView.autocorrectionType = UITextAutocorrectionTypeNo;//    typedef enum {//        UITextAutocorrectionTypeDefault, 默认//        UITextAutocorrectionTypeNo,  不自动纠错//        UITextAutocorrectionTypeYes, 自动纠错//    } UITextAutocorrectionType;    //设置text是否再次编辑就清除    textView.clearsOnBeginEditing = YES;    //设置text内容的垂直对齐方式  UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment    textView.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    //设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动    textView.adjustsFontSizeToFitWidth = YES;    //设置自动缩小显示的最小字体大小    textView.minimumFontSize = 20;    //设置键盘的样式    textView.keyboardType = UIKeyboardTypeNumberPad;//    typedef enum {//        UIKeyboardTypeDefault,      默认键盘,支持所有字符//        UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘//        UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符//        UIKeyboardTypeURL,            URL键盘,支持.com按钮 只支持URL字符//        UIKeyboardTypeNumberPad,             数字键盘//        UIKeyboardTypePhonePad,   电话键盘//        UIKeyboardTypeNamePhonePad,  电话键盘,也支持输入人名//        UIKeyboardTypeEmailAddress,  用于输入电子 邮件地址的键盘//        UIKeyboardTypeDecimalPad,    数字键盘 有数字和小数点//        UIKeyboardTypeTwitter,       优化的键盘,方便输入@、#字符//        UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,//    } UIKeyboardType;    //设置text的首字母是否大写    textView.autocapitalizationType = UITextAutocapitalizationTypeNone;//    typedef enum {//        UITextAutocapitalizationTypeNone, 不自动大写//        UITextAutocapitalizationTypeWords, 单词首字母大写//        UITextAutocapitalizationTypeSentences, 句子的首字母大写//        UITextAutocapitalizationTypeAllCharacters, 所有字母都大写//    } UITextAutocapitalizationType;    // 设置text的return键变成什么键    textView.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;    //键盘外观    textView.keyboardAppearance=UIKeyboardAppearanceDefault;//    typedef enum {//        UIKeyboardAppearanceDefault, 默认外观,浅灰色//        UIKeyboardAppearanceAlert,   深灰 石墨色//    } UIReturnKeyType;    //设置text左侧view    textView.leftView=[[UIView alloc]init];    textView.leftViewMode=UITextFieldViewModeAlways;    //设置text右侧是否加view    UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];    textView.rightView=image;    textView.rightViewMode = UITextFieldViewModeAlways;    //    typedef enum {    //        UITextFieldViewModeNever,    //        UITextFieldViewModeWhileEditing,    //        UITextFieldViewModeUnlessEditing,    //        UITextFieldViewModeAlways    //    } UITextFieldViewMode;    //设置代理 用于实现协议    textView.delegate = self;    [self.view addSubview:textView];  }//text开始编辑之前- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{    return YES;}//text开始编辑之后- (void)textFieldDidBeginEditing:(UITextField *)textField{}//text结束编辑之前- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{    return YES;}//text结束编辑之后- (void)textFieldDidEndEditing:(UITextField *)textField{}//text限制输入的内容,例如输入超过规定的字数100,就不再让输入- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{    //当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。    //这对于想要加入撤销选项的应用程序特别有用    //可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。    //要防止文字被改变可以返回NO    //这个方法的参数中有一个NSRange对象,指明了被改变文字的位置,建议修改的文本也在其中    if (range.location>=100)    {        return  NO;    }    else    {        return YES;    }}//返回一个BOOL值指明是否允许根据用户请求清除内容//可以设置在特定条件下才允许清除内容- (BOOL)textFieldShouldClear:(UITextField *)textField{    return YES;}//text键盘的return按键的触发事件- (BOOL)textFieldShouldReturn:(UITextField *)textField{    //调用这个方法,可以使键盘退下    [textField resignFirstResponder];    return YES;}
0 0