博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TextField 使用与方法总结
阅读量:6872 次
发布时间:2019-06-26

本文共 5111 字,大约阅读时间需要 17 分钟。

hot3.png

//1、    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 260, 40)];    //2、//    textField.backgroundColor = [UIColor redColor];        //属性    //设置文字颜色:textColor    textField.textColor = [UIColor redColor];    //边框状态:borderStyle    /*     1、UITextBorderStyleRoundedRect   圆角     2、UITextBorderStyleLine          黑色直角边框     3、UITextBorderStyleBezel         灰色直角边框     4、UITextBorderStyleNone          无边框     */    textField.borderStyle = UITextBorderStyleRoundedRect;    //文字水平对齐:textAlignment   默认左对齐    textField.textAlignment = NSTextAlignmentLeft;    //文字垂直对齐:contentVerticalAlignment    默认居中    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    //设置字号:font    textField.font = [UIFont systemFontOfSize:20.0];    //自适应文字大小:adjustsFontSizeToFitWidth    textField.adjustsFontSizeToFitWidth = YES;    //设置最小字号:minimumFontSize    textField.minimumFontSize = 20.0;    //默认文字:text    textField.text = @"hello zhaoliang";    //提示文字:placeholder    textField.placeholder = @"请输入账号";    //密文:secureTextEntry    textField.secureTextEntry = NO;    //输入框是否可用:enabled    textField.enabled = YES;    //return键的类型:returnKeyType    /*     1、UIReturnKeyDefault         return     2、UIReturnKeyDone            Done     3、UIReturnKeyEmergencyCall   EmergencyCall     4、UIReturnKeyGoogle=UIReturnKeyYahoo=UIReturnKeySearch   Search     5、UIReturnKeyJoin   Join     .     .     .     */    textField.returnKeyType = UIReturnKeyJoin;    //键盘类型:keyboardType    /*     1、UIKeyboardTypeDefault   数字,符号,中英文     2、UIKeyboardTypeNumberPad   数字键盘     3、UIKeyboardTypeWebSearch  网址  .     4、UIKeyboardTypeURL     .com  /   .     5、UIKeyboardTypeEmailAddress   @  .     6、UIKeyboardTypeNumbersAndPunctuation   数字 符号     .     .     .     */    textField.keyboardType = UIKeyboardTypeDefault;    //键盘颜色:keyboardAppearance    /*     1、UIKeyboardAppearanceDefault=UIKeyboardAppearanceLight  浅灰色     2、UIKeyboardAppearanceAlert=UIKeyboardAppearanceDark   深灰色     */    textField.keyboardAppearance = UIKeyboardAppearanceDefault;    //背景图片:background    当边框状态为圆角时,背景图片无效    textField.background = [UIImage imageNamed:@"map.png"];    //一键清除按钮:clearButtonMode    /*     1、UITextFieldViewModeAlways         一直出现     2、UITextFieldViewModeNever          永不出现     3、UITextFieldViewModeWhileEditing   输入框编辑文字时出现,不编辑时消失     4、UITextFieldViewModeUnlessEditing  输入框编辑文字时消失,不编辑时出现     */    textField.clearButtonMode = UITextFieldViewModeUnlessEditing;    //再次编辑是否清空之前的文字:clearsOnBeginEditing   YES:清空    textField.clearsOnBeginEditing = YES;    //是否自动大写:autocapitalizationType    /*     1、UITextAutocapitalizationTypeAllCharacters   所有字母都大写     2、UITextAutocapitalizationTypeNone    所有字母都不大写     3、UITextAutocapitalizationTypeSentences   每个句子的首字母大写     4、UITextAutocapitalizationTypeWords   每个单词的首字母大写     */    textField.autocapitalizationType = UITextAutocapitalizationTypeWords;    //是否自动纠错:autocorrectionType    /*     1、UITextAutocorrectionTypeDefault   默认     2、UITextAutocorrectionTypeNo   不纠错     3、UITextAutocorrectionTypeYes   纠错     */    textField.autocorrectionType = UITextAutocorrectionTypeYes;    //设置左视图:leftView    //设置位置无效    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];    view.backgroundColor = [UIColor redColor];    textField.leftView = view;    //左视图出现状态:leftViewMode    textField.leftViewMode = UITextFieldViewModeAlways;    //设置右视图:rightView    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];    view2.backgroundColor = [UIColor yellowColor];//    textField.rightView = view2;    //右视图出现状态:rightViewMode//    textField.rightViewMode = UITextFieldViewModeAlways;        //不同的位置,不能用相同的view        //键盘上面的view:inputAccessoryView    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];    view3.backgroundColor = [UIColor purpleColor];    textField.inputAccessoryView = view3;        //获取输入框里面的文字:textField.text    NSString *str = textField.text;    NSLog(@"%@",str);        /**************!!!!!!delegate!!!!!!!*************/    textField.delegate = self;        [textField becomeFirstResponder];        //3、    [self.view addSubview:textField];1.2、代理方法#pragma mark - UITextFieldDelegate//return键的方法- (BOOL)textFieldShouldReturn:(UITextField *)textField{    //输入框失去响应:键盘收回,光标消失    [textField resignFirstResponder];        //输入框开始响应:键盘出现,光标出现//    [textField becomeFirstResponder];        return YES;}//一键删除按钮的方法- (BOOL)textFieldShouldClear:(UITextField *)textField{    //返回YES:一键删除有效   返回NO:一键删除无效        if ([textField.text isEqualToString:@"123"]) {        return NO;    }    return YES;}//输入框是否可以开始编辑- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{    return YES;}//输入框是否可以结束编辑   **- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{    if (textField.text.length < 11) {        return NO;    }    return YES;}//光标出现就开始执行- (void)textFieldDidBeginEditing:(UITextField *)textField{    NSLog(@"textFieldDidBeginEditing");}//光标消失就执行- (void)textFieldDidEndEditing:(UITextField *)textField{    NSLog(@"textFieldDidEndEditing");}

 

转载于:https://my.oschina.net/jimolengsha/blog/709746

你可能感兴趣的文章
mongo官方企业版安装及数据库授权使用
查看>>
推荐开发人员看的具有影响力的书籍
查看>>
[数]昨天欠下的一道立体几何题HDU-4741
查看>>
重装wordpress
查看>>
MapReduce 学习(一)
查看>>
专访李智慧:架构是最高层次的规划和难以改变的决定
查看>>
HTML5 入门基础
查看>>
Laravel 中的 Many-To-Many
查看>>
Codeforces 371C Hamburgers(二分基础题)
查看>>
django 自定义tag和filter
查看>>
FileWriter写数据路径问题及关闭和刷新方法的区别
查看>>
Page Layout里的javascript (jquery)不执行
查看>>
JS中的发布订阅模式
查看>>
解析JMeter的JTL文件
查看>>
1-N中1出现的次数
查看>>
springmvc自定义视图
查看>>
windows driver 映射大文件
查看>>
《R语言实战》读书笔记--第三章 图形初阶(一)
查看>>
MFC串口的编程 mscomm控件与SerialPort类
查看>>
乔恩与加菲猫引发的思考
查看>>