UI03_UITextField

来源:互联网 发布:上网便宜的手机卡知乎 编辑:程序博客网 时间:2024/06/14 00:29
#import "AppDelegate.h"14.签订协议@interface AppDelegate ()<UITextFieldDelegate>1.创建两个UITextField的属性.@property(nonatomic, retain) UITextField *textField;@property(nonatomic, retain) UITextField *secondField;@end@implementation AppDelegate- (void)dealloc{    [_secondText release];    [_textField release];    [_window release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    [_window release];2.创建textFieldself.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];self.textField.backgroundColor = [UIColor cyanColor];[self.window addSubview:self.textField];[self.textField release];3.添加边框和弧度.self.textField.layer.borderWidth = 1;self.textField.layer.cornerRadius = 10;4.显示文本内容.self.textField.text = @“Messi";

显示内容

5.显示文本颜色.self.textField.textColor = [UIColor greenColor];

显示内容

6.文本对齐方式.self.textField.textAlignment = NSTextAlicgnmentCenter;

对齐方式

7.设置文本字体.self.textField.font = [UIFont systemFontOfSize:25];

设置字体

8.占位符.self.textField.placeholder = @"MM";

占位符

9.控制能否使用输入框(为NO时,无法输入).self.textField.enabled = YES;10.密码可见度.self.textField.secureTextEntry = YES;

密码可见度

11.键盘类型.self.textField.keyboardType = UIKeyboardTypeNumberPad;

数字键盘

12.改变return按钮的样式.self.textField.returnKeyType = UIReturnKeyYahoo;

改变结果

13.点击return回收键盘.15.设置代理人.self.textField.delegate = self;// 点击return回收键盘,注意该方法要放到大括号外面写.- (BOOL)textFieldShouldReturn:(UITextField *)textField {    NSLog(@"11111");    // 失去第一响应者.    [textField resignFirstResponder];    return YES;}16.通过自定义视图,取代键盘UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];view.backgroundColor = [UIColor yellowColor];self.textField.inputView = view;

取代键盘

17.添加推广效果(添加该效果的时候要去掉上面的 self.textField.inputView = view;).self.textField.inputAccessoryView = view;

效果

18.添加清除按钮.self.textField.clearButtonMode = UITextFieldViewModeAlways;

tia添加结果

19.给textField添加一个事件.[self.textField addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventEditingChanged];// 打印此时的地址.NSLog(@"%p", self.textField);// 创建一个UILabel,并给定一个tag值.UILabel *first = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 200, 40)];first.layer.borderWidth = 1;first.layer.cornerRadius = 10;[self.window addSubview:first];[first release];first.tag = 1000;// 创建一个Secondtext,同时给它添加一个事件;控件写成属性一定要使用.self.secondText = [[UITextField alloc] initWithFrame:CGRectMake(100, 300, 150, 50)];self.secondText.backgroundColor = [UIColor greenColor];[self.window addSubview:self.secondText];[self.secondText release];// 给Secondtext添加一个事件[self.secondText addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventEditingChanged];// 此时可以看出Secondtext与textField关联的是同一个事件(valueChange:).// 打印此时的地址.NSLog(@"%p", self.secondText);#warning 在给某些控件绑定方法的时候,一般会在写方法的时候给一个同类型的参数,哪个控件去执行方法,对应的参数就是哪个对象,省去寻找触发事件的对象的麻烦.    return YES;}- (void)valueChange:(UITextField *)text {    // 打印传进来的参数地址.    NSLog(@"%p", text);    // 在控制台显示出入的内容    NSLog(@"%@", text.text);    // 通过tag值来寻找指定的label,因为[]中的类型为UIView,接收对象的类型为UILabel,所以要强制类型转换.    UILabel *label = (UILabel *)[self.window viewWithTag:1000];    // 判断两个textField中的内容相同,并在中间的Label中显示结果.    if ([self.secondText.text isEqualToString:self.textField.text]) {        label.text = @"相同";    } else {        label.text = @"不同";    }}

打印前后的结果

创建两个UITextField,实现弹出键盘,原本被覆盖的文本框弹上去相应的距离    1.第一个文本框.    UITextField *firstTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 300, 150, 50)];    firstTextField.layer.borderWidth = 1;    firstTextField.layer.cornerRadius = 10;    [self.view addSubview:firstTextField];    [firstTextField release];    2.第二个文本框.    UITextField *secondTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 400, 150, 50)];    secondTextField.layer.borderWidth = 1;    secondTextField.layer.cornerRadius = 10;    [self.view addSubview:secondTextField];    [secondTextField release];    // 设置代理人.    secondTextField.delegate = self;    // 设置清除按钮的样式.    secondTextField.clearButtonMode = UITextFieldViewModeAlways;3.点击清除按钮会触发这个协议方法(第二个文本框中).- (BOOL)textFieldShouldClear:(UITextField *)textField {    NSLog(@"内容清除");    return YES;}4.输入框将要开始进行编辑(配合结束编辑使用).- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {    if (textField.frame.origin.y > HEIGHT / 2) {        // 计算当前textField和屏幕中心的距离.        CGFloat height = textField.frame.origin.y - HEIGHT / 2;        // 向上移动的距离        self.view.center = CGPointMake(self.view.center.x, self.view.center.y - height);    }    return YES;}5.结束编辑的时候执行这个方法(配合开始编辑使用).- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {    if (textField.frame.origin.y > HEIGHT / 2) {        // 计算当前textField和屏幕中心的距离.        CGFloat height = textField.frame.origin.y - HEIGHT / 2;        // 当编辑结束时,回到原来位置.        self.view.center = CGPointMake(self.view.center.x, self.view.center.y + height);    }    return YES;}

效果图

6.实时监控文本框中的输入结果.- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {    NSLog(@"textField:%@", textField.text);    NSLog(@"string:%@", string);    return YES;}点击空白处回收键盘.创建RootViewController类.RootViewController.m1.创建一个UITextField类的对象UITextField *secondButton = [[UITextField alloc] initWithFrame:CGRectMake(150, 150, 100, 100)];secondButton.backgroundColor = [UIColor greenColor];[firstView addSubview:secondButton];[secondButton release];[secondButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];2.使secondButton成为第一响应者.[secondButton becomeFirstResponder];   // 设置其为第一响应者之后,当程序运行后键盘默认会弹出.secondButton.tag = 1000;3.- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    [super touchesBegan:touches withEvent:event];        // 通过tag值先找textField.实现点击空白处回收键盘.    UITextField *textField = (UITextField *)[self.view viewWithTag:1000];    [textField resignFirstResponder];}
0 0
原创粉丝点击