zoukankan      html  css  js  c++  java
  • IOS复习UITextfield&UILabel

    首先新建一个Single View Application项目



    再配置如下



     

    打开chenViewController.h

    添加<UITextFieldDelegate>协议

    如下

    @interface chenViewController : UIViewController<UITextFieldDelegate>
    //添加UITextFieldDelegate协议,因为TextField里输入内容时需要对键盘进行一些设置。
     
    @end
    打开
    chenViewController.m
    添加代码如下
    - (void)viewDidLoad
    {
        [superviewDidLoad];
        //标题
        //实例化
        UILabel *qqLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 80, 30)];
        //设置文字
        [qqLabel setText:@"QQ"];
        //设置文字颜色
        [qqLabel setTextColor:[UIColorredColor]];
        //设置背景色
        [qqLabel setBackgroundColor:[UIColorclearColor]];
        //添加到视图
        [self.view addSubview:qqLabel];
        //帐号label
        UILabel *qqIDLabel=[[UILabel alloc]initWithFrame:CGRectMake(40, 80, 80, 30)];
        [qqIDLabel setText:@"帐号"];
        [qqIDLabel setTextColor:[UIColorblackColor]];
        [qqIDLabel setBackgroundColor:[UIColorclearColor]];//clearColor是清空颜色,就是无色
        [self.view addSubview:qqIDLabel];
        //密码label
        UILabel *qqPWLabel=[[UILabel alloc]initWithFrame:CGRectMake(40, 120, 80, 30)];
        [qqPWLabel setText:@"密码"];
        [qqPWLabel setTextColor:[UIColorblackColor]];
        [qqPWLabel setBackgroundColor:[UIColorclearColor]];
        [self.view addSubview:qqPWLabel];
        //帐号text
        //实例化
        UITextField *qqIDtext=[[UITextField alloc]initWithFrame:CGRectMake(100, 80, 180, 30)];
        //设置占位符,
        qqIDtext.placeholder=@"请输入你的帐号";
        //设置样式,
        qqIDtext.borderStyle=UITextBorderStyleRoundedRect;
        //设置键盘Done样式
        qqIDtext.returnKeyType=UIReturnKeyDone;
        //委托类需要遵守UITextFieldDelegate协议
        qqIDtext.delegate=self;
        //添加事件,resignF:
        [qqIDtext addTarget:selfaction:@selector(resignF) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:qqIDtext];
        //密码text
        UITextField *qqPWtext=[[UITextField alloc]initWithFrame:CGRectMake(100, 120, 180, 30)];
        qqPWtext.borderStyle=UITextBorderStyleRoundedRect;
        qqPWtext.returnKeyType=UIReturnKeyDone;
        qqPWtext.secureTextEntry=YES;
        //电话键盘样式
        qqPWtext.keyboardType=UIKeyboardTypeNumberPad;
        qqPWtext.delegate=self;
        [qqPWtext addTarget:selfaction:@selector(textFieldShouldReturn:) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:qqPWtext];
    }
    -(void)resignF
    {
    }
    //按下Done按钮时调用这个方法,可让按钮消失
    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [textField resignFirstResponder];
        returnYES;
    }
    但是有时候键盘挡住了输入框,下面详细介绍这两个方法
    -(void)textFieldDidBeginEditing:(UITextField *)textField
    {
        //键盘高度216
        if (textField.frame.origin.y>216){
            
            CGRect frame=self.view.frame;
            frame.origin.y-=216;
            frame.size.height+=216;
            self.view.frame=frame;
            }
        
    }
    //当对textField编辑结束时调用此方法,需要调整视图为初始位置
    -(void)textFieldDidEndEditing:(UITextField *)textField
    {
        
        CGRectf rame=self.view.frame;
        frame.origin.x=00;
        frame.origin.y=20;//为什么20,因为iphone视图最上面通知栏20个像素
        frame.size.height =480;
        self.view.frame=frame;
    }
     
    一、键盘风格
    UIKit框架支持8种风格键盘。
    typedefenum {
        UIKeyboardTypeDefault,                // 默认键盘:支持所有字符
        UIKeyboardTypeASCIICapable,           // 支持ASCII的默认键盘
        UIKeyboardTypeNumbersAndPunctuation,  // 标准电话键盘,支持+*#等符号
        UIKeyboardTypeURL,                    // URL键盘,有.com按钮;只支持URL字符
        UIKeyboardTypeNumberPad,              //数字键盘
        UIKeyboardTypePhonePad,               // 电话键盘
        UIKeyboardTypeNamePhonePad,           // 电话键盘,也支持输入人名字
        UIKeyboardTypeEmailAddress,           // 用于输入电子邮件地址的键盘
    } UIKeyboardType;
    用法用例:
    textView.keyboardtype = UIKeyboardTypeNumberPad;
    二、键盘外观
    typedefenum {
        UIKeyboardAppearanceDefault,    // 默认外观:浅灰色
        UIKeyboardAppearanceAlert,      //深灰/石墨色
    } UIKeyboardAppearance;
    用法用例:
    textView.keyboardAppearance=UIKeyboardAppearanceDefault;
    三、回车键
    typedef enum {
            UIReturnKeyDefault,  //默认:灰色按钮,标有Return
            UIReturnKeyGo,  //标有Go的蓝色按钮
            UIReturnKeyGoogle,  //标有Google的蓝色按钮,用于搜索
            UIReturnKeyJoin,  //标有Join的蓝色按钮
            UIReturnKeyNext,  //标有Next的蓝色按钮
            UIReturnKeyRoute,  //标有Route的蓝色按钮
            UIReturnKeySearch,  //标有Search的蓝色按钮
            UIReturnKeySend,  //标有Send的蓝色按钮
            UIReturnKeyYahoo,  //标有Yahoo!的蓝色按钮,用于搜索
            UIReturnKeyDone,  //标有Done的蓝色按钮
            UIReturnKeyEmergencyCall,  //紧急呼叫按钮
    } UIReturnKeyType;
    用法用例:
    textView.returnKeyType=UIReturnKeyGo;
    四、自动大写
    typedefenum {
        UITextAutocapitalizationTypeNone, //不自动大写
        UITextAutocapitalizationTypeWords, //单词首字母大写
        UITextAutocapitalizationTypeSentences, //句子首字母大写
        UITextAutocapitalizationTypeAllCharacters, //所有字母大写
    } UITextAutocapitalizationType;
    用法用例:
    textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    五、自动更正
    typedefenum {
        UITextAutocorrectionTypeDefault,//默认
        UITextAutocorrectionTypeNo,//不自动更正
        UITextAutocorrectionTypeYes,//自动更正
    } UITextAutocorrectionType;
    用法用例:
    textField.autocorrectionType = UITextAutocorrectionTypeYes;
    六、安全文本输入
    textView.secureTextEntry=YES;
    开启安全输入主要是用于密码或一些私人数据的输入,此时会禁用自动更正和自此缓存。
    七、键盘遮住视图
    这个问题又来已久,我专门写了篇文章来解决此问题,请笑纳:《打开键盘遮住View的问题解决方法》
    默认情况下打开键盘会遮住下面的view,带来一点点困扰,不过这不是什么大问题,我们使用点小小的手段就可以解决。
    首先我们要知道键盘的高度是固定不变的,不过在IOS 5.0以后键盘的高度貌似不是216了,不过不要紧,我们调整调整就是了:
    iPhone   ipad
    竖屏(portrait)
    216264
    横屏(landScape)
    140    352
     
    我们采取的方法就是在textField(有可能是其他控件)接收到弹出键盘事件时把self.view整体上移216px了(我们就以iPhone竖屏为例了)。
    有关View的frame,origin,size之类的知识点不懂的请参看我的另一篇博文: <<有关View的几个基础知识点>>
    首先我们要设置textField的代理,我们就设为当前控制器了。
    textField,delegate=self;
    然后我们在当前控制器实现下面三个委托方法:
    - (void)textFieldDidBeginEditing:(UITextField *)textField
    { //当点触textField内部,开始编辑都会调用这个方法。textField将成为first responder
               NSTimeInterval animationDuration = 0.30f;
              CGRect frame = self.view.frame;
              frame.origin.y -=216;
              frame.size.height +=216;
              self.view.frame = frame;
               [UIView beginAnimations:@"ResizeView" context:nil];
               [UIView setAnimationDuration:animationDuration];
               self.view.frame = frame;
               [UIView commitAnimations];
    }
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {//当用户按下ruturn,把焦点从textField移开那么键盘就会消失了
                NSTimeInterval animationDuration = 0.30f;
                CGRect frame = self.view.frame;
                frame.origin.y +=216;
                frame.size. height -=216;
                self.view.frame = frame;
            //self.view移回原位置
            [UIView beginAnimations:@"ResizeView" context:nil];
            [UIView setAnimationDuration:animationDuration];
                self.view.frame = frame;
                [UIView commitAnimations];
                [textField resignFirstResponder];
    }
    3.例举一个Lable的代码属性及功能的实现过程
    初始化lable的位置和大小,CGRectMake()函数中的参数分别是原点坐标x和y,后面两个参数表示lable的宽度(width)和高度(height),
    studentLable=[[UILabelalloc]initWithFrame:CGRectMake(100,10,100,30)];
    设置Lable的字体颜色值颜色,
    [studentLable setTextColor:[UIColor blackColor]];
    设置lable字体大小
    studentLable.font=[UIFontsystemFontOfSize:20];
    设置lable的背景颜色
    studentLable.backgroundColor = [UIColor redColor];
    设置lable的标题字体对齐方式,此处居中对其
    [studentLable setTextAlignment:UITextAlignmentCenter];
    设置lable标题
    studentLable.text=@"学 生";
    把lable添加到视图上,少了这一步视图上使显示不出来的
    [self.view addSubview:studentLable];
    4.例举一个TextField的代码的实现其属性和功能的过程
    //初始化坐标位置
    sNameTextField=[[UITextFieldalloc]initWithFrame:CGRectMake(100,60,200,30)];
    //为空白文本字段绘制一个灰色字符串作为占位符,提示作用
    sNameTextField.placeholder =@"输入姓名";
     //默认就是左对齐,这个是UITextField扩展属性
    sNameTextField.textAlignment =UITextAlignmentLeft;
     //设置textField的形状
    sNameTextField.borderStyle=UITextBorderStyleRoundedRect;
    //设置键盘完成按钮
     
    sNameTextField.returnKeyType=UIReturnKeyDone;
    //委托类需要遵守UITextFieldDelegate协议
    sNameTextField.delegate=self;
    //设置TextFiel输入框字体大小
    sNameTextField.font = [UIFontsystemFontOfSize:18];
    //安全设置,密文保护设置,显示点点,常用于密码设置栏
    sNameTextField.secureTextEntry = YES;
     
    //   把TextField添加到视图上
     [self.viewaddSubview:sNameTextField];
     
    //输入年龄需要启用数字键盘,此处实现自动跳转到数字键盘
     
    sAgeTextField.keyboardType =UIKeyboardTypeNumberPad;
    5.介绍完lable和TextField代码实现,打开LTFViewController.m文件在ViewDidLoad中添加初始化代码,
     
    - (void)viewDidLoad
    {
            [super viewDidLoad];
            // Do any additional setup after loading the view, typically from a nib.
            studentLable=[[UILabel alloc] initWithFrame:CGRectMake(100, 10, 100, 30)];
            sNameLable=[[UILabel alloc] initWithFrame:CGRectMake(10, 60, 80, 30)];
            sSexLable=[[UILabel alloc] initWithFrame:CGRectMake(10, 100, 80, 30)];
            sAgeLable=[[UILabel alloc] initWithFrame:CGRectMake(10, 140, 80, 30)];
        
            teacherLable=[[UILabel alloc] initWithFrame:CGRectMake(100, 200, 100, 30)];
            tNameLable=[[UILabel alloc] initWithFrame:CGRectMake(10,240, 80, 30)];
            tSexLable=[[UILabel alloc] initWithFrame:CGRectMake(10, 280, 80, 30)];
        
        
            [studentLable setTextColor:[UIColor blackColor]];
            [sNameLable setTextColor:[UIColor blackColor]];
            [sAgeLable setTextColor:[UIColor blackColor]];
            [sSexLable setTextColor:[UIColor blackColor]];
        
            [teacherLable setTextColor:[UIColor blackColor]];
            [tNameLable setTextColor:[UIColor blackColor]];
            [tSexLable setTextColor:[UIColor blackColor]];
        
        
            studentLable.backgroundColor = [UIColor redColor];
            sNameLable.backgroundColor = [UIColor clearColor];
            sAgeLable.backgroundColor=[UIColor clearColor];
            sSexLable.backgroundColor = [UIColor clearColor];
            teacherLable.backgroundColor = [UIColor blueColor];
            tNameLable.backgroundColor = [UIColor clearColor];
            tSexLable.backgroundColor = [UIColor clearColor];
        
            [studentLable setTextAlignment:UITextAlignmentCenter];
            [sNameLable setTextAlignment:UITextAlignmentCenter];
            [sAgeLable setTextAlignment:UITextAlignmentCenter];
            [sSexLable setTextAlignment:UITextAlignmentCenter];
        
        
            [teacherLable setTextAlignment:UITextAlignmentCenter];
            [tNameLable setTextAlignment:UITextAlignmentCenter];
            [tSexLable setTextAlignment:UITextAlignmentCenter];
        
            studentLable.text=@"学  生";
            sNameLable.text=@"姓名:";
            sAgeLable.text=@"性别:";
            sSexLable.text=@"年龄:";
        
            teacherLable.text=@"老  师";
            tNameLable.text=@"姓名:";
            tSexLable.text=@"性别:";
        
            studentLable.font=[UIFont systemFontOfSize:20];
        
            [self.view addSubview:studentLable];
            [self.view addSubview:sNameLable];
            [self.view addSubview:sAgeLable];
            [self.view addSubview:sSexLable];
            [self.view addSubview:teacherLable];
            [self.view addSubview:tNameLable];
            [self.view addSubview:tSexLable];
        
            sNameTextField=[[UITextField alloc] initWithFrame:CGRectMake(100, 60, 200, 30)];//初始化坐标位置
        
            sNameTextField.placeholder = @"输入姓名";//为空白文本字段绘制一个灰色字符串作为占位符
        
            sNameTextField.textAlignment = UITextAlignmentLeft;//默认就是左对齐,这个是UITextField扩展属性
        
            sNameTextField.borderStyle=UITextBorderStyleRoundedRect;  //设置textField的形状
        
            //    sNameTextField.clearsOnBeginEditing = NO;//设置为YES当用点触文本字段时,字段内容会被清除,这个属性一般用于密码设置,当输入有误时情况textField中的内容
        
            sNameTextField.returnKeyType=UIReturnKeyDone;//设置键盘完成按钮
        
            sNameTextField.delegate=self;//委托类需要遵守UITextFieldDelegate协议
        
            sNameTextField.font = [UIFont systemFontOfSize:18]; //设置TextFiel输入框字体大小
        
        
            sAgeTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 30)];
            sAgeTextField.textAlignment = UITextAlignmentLeft;
            sAgeTextField.borderStyle = UITextBorderStyleRoundedRect;
            sAgeTextField.keyboardType = UIKeyboardTypeNumberPad;//输入年龄需要设置数字键盘
        
            [sAgeTextField addTarget:self action:@selector(textFieldDone:)  forControlEvents:UIControlEventTouchDown]; //用textFieldDone函数,实现关闭数字键盘
            sAgeTextField.delegate=self;
        
        
            sSexTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 140, 200, 30)];
            sSexTextField.textAlignment = UITextAlignmentLeft;
            sSexTextField.borderStyle = UITextBorderStyleRoundedRect;
            sSexTextField.returnKeyType=UIReturnKeyDone;
            sSexTextField.delegate=self;
        
        
            //  sNameTextField.enabled=NO;  //把 sNameTextField设置成无效,点击任何反应
        
            tNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 240, 200, 30)];
            tNameTextField.textAlignment = UITextAlignmentLeft;
            tNameTextField.borderStyle = UITextBorderStyleRoundedRect;
        
            tNameTextField.delegate=self;
        
        
            tSexTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 280, 200, 30)];
            tSexTextField.textAlignment = UITextAlignmentLeft;
            tSexTextField.borderStyle = UITextBorderStyleRoundedRect;
            tSexTextField.returnKeyType=UIReturnKeyDone;
            tSexTextField.delegate=self;
            tSexTextField.secureTextEntry = YES;  //安全设置,密文保护设置
        
            //    测试用的TextField
            UITextField *testTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 400, 200, 30)];
            testTextField.textAlignment = UITextAlignmentLeft;
            testTextField.borderStyle = UITextBorderStyleRoundedRect;
            testTextField.returnKeyType=UIReturnKeyDone;
        
        
            testTextField.delegate=self;
        
            //    把TextField添加到视图上
            [self.view addSubview:sNameTextField];
            [self.view addSubview:sAgeTextField];
            [self.view addSubview:sSexTextField];
            [self.view addSubview:tSexTextField];
            [self.view addSubview:tNameTextField];
            [self.view addSubview:testTextField];
    }
     
    6.然后用代理方法处理的就是键盘的操作,因为当键盘弹起的时候,在最下面的lable和textField可能会键盘遮挡,
     
    在ViewDidLoad中我们有这样sNameTextField.returnKeyType=UIReturnKeyDone一段代码,作用就是当编辑完成后键盘右下角出现一个Done键,如果是中文输入法出现的是确定键,然后调用代理方法textFieldShouldReturn,当我们按下Done时,键盘就会退出;
    resignFirstResponder(交出自己的第一响应者的身份,可以重写函数,这样如果写出返回no的时候当调用这个方法的时候它会拒绝交出第一响应者的身份)
     
    [cpp] view plaincopy
     
    //按下Done按钮时调用这个方法,可让按钮消失
    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        
            [textField resignFirstResponder];
            return YES;
    }
    7.当需要输入数字时候的时候 sAgeTextField.keyboardType = UIKeyboardTypeNumberPad;可以自动切换到数字键盘,键盘的八种风格
     
    typedefenum {
            UIKeyboardTypeDefault,               //默认键盘:支持所有字符
            UIKeyboardTypeASCIICapable,          //支持ASCII的默认键盘
            UIKeyboardTypeNumbersAndPunctuation, //标准电话键盘,支持+*#等符号
            UIKeyboardTypeURL,                   // URL键盘,有.com按钮;只支持URL字符
            UIKeyboardTypeNumberPad,             //数字键盘
            UIKeyboardTypePhonePad,              //电话键盘
            UIKeyboardTypeNamePhonePad,          //电话键盘,也支持输入人名字
            UIKeyboardTypeEmailAddress,          //用于输入电子邮件地址的键盘
    } UIKeyboar;
    8.处理键盘遮挡视图的方法:
     
    当开始点击textField会调用的方法,键盘高度216,当触摸高度在216一下extField时候,调整视图,把视图纵坐标向上增加216,orign(0,-216),然后view视图的高度增加216像素,相当于视图为320*696像素,如果设置增加216像素给view视图,会出现键盘遮盖部分为一篇黑色区域背景,当退出键盘是黑色会显示出来,给人视觉效果体验不好;当对textField编辑结束时调用textFieldDidEndEditing方法,调整视图为初始位置,frame.origin.y =20;为什么把视图的原点坐标纵坐标设置为20,因为iphone视图最上面通知栏占了20个像素;
     
    [cpp] view plaincopy
     
    -(void)textFieldDidBeginEditing:(UITextField *)textField
    {
        //    键盘高度216
            if (textField.frame.origin.y>216) {
            
                CGRect frame =  self.view.frame;
                frame.origin.y -=216;
                frame.size.height +=216;
                self.view.frame=frame;
                }
        
    }
    [cpp] view plaincopy
     
    // 当对textField编辑结束时调用此方法,需要调整视图为初始位置
    -(void)textFieldDidEndEditing:(UITextField *)textField
    {
        
                CGRect frame = self.view.frame;
                frame.origin.x=00;
                frame.origin.y =20;//为什么20,因为iphone视图最上面通知栏20个像素
                frame.size.height =480;
                self.view.frame=frame;
        
          }
     
    9再附上其他几个委托方法源码
     
     
    [cpp] view plaincopy
     
    //TextField的委托方法
     
    -(BOOL)textFieldShouldEndEditing:(UITextField *)textField
    {
        //返回一个BOOL型值,指定是否循序文本字段编辑
            return YES;
    }
    [cpp] view plaincopy
     
    -(BOOL)textFieldShouldClear:(UITextField *)textField
    {
        //    返回一个BOOL值指明是否允许根据用户请求清除内容  可以设置在特定条件下才允许清除内容
            return YES;
    }
     
    10.数字键盘并没有Done键,就不能退出按钮,通过[sAgeTextFieldaddTarget:selfaction:@selector(textFieldDone:) forControlEvents:UIControlEventTouchDown];
     
    这个相当于给TextField增加了一个按钮的功能,调用textFieldDone,当向输入框编辑完内容后,再次点击输入框键盘就退出了;
     
    //重写数字键盘的方法
    -(void)textFieldDone:(id)sender{
         [sAgeTextField resignFirstResponder];
    }
    上面很多的都是从别人博客整合修改的,,例子是我自己修改的,下次把我自己学习的博客,视频,以及书籍和出现问题搜索的方法(解决问题的途经)单独写出来
    运行结果如下图,输入密码是密文

                           2013年8月7日,9:50左右,东南大学无锡分校桃园3宿舍106室

     查api发现UITextAlignmentCenter变成NSTextAlignmentCenter

    [money setTextAlignment:NSTextAlignmentCenter];

  • 相关阅读:
    OpenWrt的luci web管理器添加新菜单
    Lua基础
    2016年1月25日 《1024伐木累》-小白篇之开发网站,三天!(中篇-2奇怪的IE)-总章节十一
    《1024伐木累》-程序员妹子与花木兰
    《1024伐木累》-关注女神小号,藏大钱
    《1024伐木累》-小白篇之开发网站,三天!(中篇-1)-总章节十
    《1024伐木累》-炒股赚钱,大保健
    《1024伐木累》-找规律,女生不讲卫生~
    微信支付开发-Senparc.Weixin.MP详解
    《1024伐木累》-小白篇之开发网站,三天!(前篇)-总章节八
  • 原文地址:https://www.cnblogs.com/ioschen/p/3248819.html
Copyright © 2011-2022 走看看