`

新风作浪博客学习(二)代码实现Lable 、textField创建界面以及键盘的处理

    博客分类:
  • ios
 
阅读更多
今天写的是用代码实现一个简单界面,代码重复率比较高,可读性不是太好,但是适合初学者看看,实现的简单功能界面:


[img]

[/img]



1.创建一个新工程叫LTFDemo; File->New->Project ->single View Application -> next
[img]

[/img]



2.打开LTFViewController.h文件,添加UITextFieldDelegate协议,因为TextField里输入内容时需要对键盘进行一些设置。界面需要一些布局,在LTFViewController.h文件一些声明对象
<span style="font-size:12px;">#import <UIKit/UIKit.h>

@interface LTFViewController : UIViewController<UITextFieldDelegate>
{
    UILabel *studentLable;//设置学生标题
    UILabel *sNameLable;//学生姓名
    UILabel *sSexLable;//学生性别
    UILabel *sAgeLable;//学生年龄
    
    UILabel *teacherLable;//老师标题
    UILabel *tNameLable;//老师姓名
    UILabel *tSexLable;//老师姓名
    
    UITextField *sNameTextField;//学生姓名输入框
    UITextField *sAgeTextField;//学生年龄输入框
    UITextField *sSexTextField;//学生性别输入框
    UITextField *tNameTextField;//老师姓名输入框   
    UITextField *tSexTextField; //老师性别输入框
}

@end</span>





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的时候当调用这个方法的时候它会拒绝交出第一响应者的身份)
//按下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个像素;
-(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
{

        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再附上其他几个委托方法源码:
//TextField的委托方法

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
//返回一个BOOL型值,指定是否循序文本字段编辑
    return YES;
}


-(BOOL)textFieldShouldClear:(UITextField *)textField
{
//    返回一个BOOL值指明是否允许根据用户请求清除内容  可以设置在特定条件下才允许清除内容  
    return YES;
}






10.数字键盘并没有Done键,就不能退出按钮,通过[sAgeTextFieldaddTarget:selfaction:@selector(textFieldDone:) forControlEvents:UIControlEventTouchDown];

这个相当于给TextField增加了一个按钮的功能,调用textFieldDone,当向输入框编辑完内容后,再次点击输入框键盘就退出了;
//重写数字键盘的方法
-(void)textFieldDone:(id)sender{
 [sAgeTextField resignFirstResponder];
}



  • 大小: 119.7 KB
  • 大小: 180.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics