UITextField常用方法

AppDelegate.h

1 #import <UIKit/UIKit.h>
2 #import "ViewController.h"
3 
4 // 第二步:遵守UITextFieldDelegate协议
5 @interface AppDelegate : UIResponder <UIApplicationDelegate, UITextFieldDelegate>
6 @property (strong, nonatomic) UIWindow *window;
7 @end

AppDelegate.m

  1 #import "AppDelegate.h"
  2 
  3 @interface AppDelegate ()
  4 
  5 @end
  6 
  7 @implementation AppDelegate
  8 
  9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 10     
 11     // 创建window
 12     self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
 13     self.window.backgroundColor = [UIColor whiteColor];
 14     [self.window makeKeyAndVisible];
 15     self.window.rootViewController = [[ViewController alloc] init];
 16     
 17 #pragma mark - UITextField
 18     // 1.创建UITextField对象并初始化
 19     UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, 300, 80)];
 20     
 21     // 2.设置属性
 22     textField.backgroundColor = [UIColor lightGrayColor];  // 背景颜色
 23     textField.text = @"用户名";   // 文本内容
 24     textField.textColor = [UIColor redColor];   // 文本颜色
 25     textField.textAlignment = NSTextAlignmentCenter;   // 文本对齐方式
 26     textField.font = [UIFont fontWithName:@"Heiti SC" size:20];  // 文本字体和大小
 27     textField.borderStyle = UITextBorderStyleRoundedRect;  // 边框样式
 28     textField.placeholder = @"请输入用户名";   // 设置占位符
 29     textField.enabled = YES;  // 控制能否编辑
 30     textField.clearsOnBeginEditing = YES;    // 开始编辑时是否清空输入框的内容
 31     //textField.secureTextEntry = YES;   // 是否安全输入
 32     textField.keyboardType = UIKeyboardTypeDefault;   // 键盘模式
 33     textField.returnKeyType = UIReturnKeyDefault;   // 键盘返回值类型
 34     
 35     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 414, 200)];
 36     view.backgroundColor = [UIColor cyanColor];
 37     //textField.inputView = view;  // 自定义键盘
 38     
 39     UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 414, 100)];
 40     view1.backgroundColor = [UIColor greenColor];
 41     textField.inputAccessoryView = view1;  // 输入框上方的内容
 42     
 43     textField.clearButtonMode = UITextFieldViewModeWhileEditing;  // 清楚按钮模式(x)
 44     
 45     UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
 46     leftView.backgroundColor = [UIColor orangeColor];
 47     textField.leftView = leftView;    // 输入框左视图
 48     textField.leftViewMode = UITextFieldViewModeAlways;  // 左视图模式
 49     
 50     UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
 51     rightView.backgroundColor = [UIColor blueColor];
 52     textField.rightView = rightView;    // 输入框右视图
 53     textField.rightViewMode = UITextFieldViewModeAlways;   // 右视图模式
 54     
 55     textField.keyboardAppearance = UIKeyboardAppearanceDark;  // 键盘颜色
 56     
 57     textField.tintColor = [UIColor redColor];  // 光标的颜色
 58     
 59     
 60     [textField becomeFirstResponder];  // 成为第一响应者,鼠标不用点,就可以输入内容
 61     
 62     
 63     // 3.设置代理
 64     // 键盘回收
 65     // 第一步:设置代理
 66     textField.delegate = self;
 67     
 68     
 69     // 4.添加到父视图上
 70     [self.window addSubview:textField];
 71     
 72     // 打印所有的字体样式
 73     //NSLog(@"%@", [UIFont familyNames]);
 74     
 75     return YES;
 76 }
 77 
 78 
 79 // 第三步:实现代理方法
 80 // 将要开始编辑
 81 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
 82     NSLog(@"将要开始编辑");
 83     return YES;
 84 }
 85 
 86 // 已经开始编辑
 87 - (void)textFieldDidBeginEditing:(UITextField *)textField {
 88     NSLog(@"已经开始编辑");
 89 }
 90 
 91 // 将要结束编辑
 92 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
 93     NSLog(@"将要结束编辑");
 94     return YES;
 95 }
 96 
 97 // 已经结束编辑
 98 - (void)textFieldDidEndEditing:(UITextField *)textField {
 99     NSLog(@"已经结束编辑");
100 }
101 
102 // 当点击键盘return的时候
103 - (BOOL)textFieldShouldReturn:(UITextField *)textField {
104     [textField resignFirstResponder];  // 释放第一相应者
105     NSLog(@"当点击键盘return的时候");
106     return YES;
107 }
108 
109 @end

 

转载于:https://www.cnblogs.com/zhizunbao/p/5359667.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值