#import "AppDelegate.h"
/*
UITextField:是UIControl的子类,是文本输入框
⭐️⭐️⭐️⭐️属性⭐️⭐️⭐️⭐️
text:可以获得或改变输入框的文字内容
placeholder:提示文字 如请输入账号
textColor:
font:
textAlignment:
adjustsFontSizeToFitWidth:让文字自动根据宽度适配
minimumFontSize:设置文字最小字号
❤️❤️❤️delegate:代理❤️❤️❤️
clearButtonMode:设置清除按钮的显示时间
UITextFieldViewModeNever:永远都不显示
UITextFieldViewModeWhileEditing:当编辑时显示
UITextFieldViewModeUnlessEditing:不再编辑时显示
UITextFieldViewModeAlways:永远显示
leftView:输入框左侧视图
rightView:输入框右侧视图
❤️❤️不是设置了左右侧视图就可以显示,需要配合使用以下属性❤️❤️
leftViewMode:设置什么情况下显示左侧视图的
rightViewMode:设置什么情况下显示右侧视图的
keyboardType:键盘的样式
UIKeyboardTypeDefault
UIKeyboardTypeASCIICapable UIKeyboardTypeNumbersAndPunctuation : 数字和符号
UIKeyboardTypeURL :URL网址类型
下面三条iPad专用
UIKeyboardTypeNumberPad:只显示 0 - 9 的数字 UIKeyboardTypePhonePad:电话类型(1-9,*,0,#)
UIKeyboardTypeNamePhonePad:
UIKeyboardTypeEmailAddress:邮箱地址类型
UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1), UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),
UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0):网页搜索,小地球的样式
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
inputView:键盘上面的视图
inputAccessoryView:键盘区域的视图
borderStyle:输入框的样式
UITextBorderStyleNone
UITextBorderStyleLine:黑线边框
UITextBorderStyleBezel:黑线阴影
UITextBorderStyleRoundedRect:圆角
输入框的方法(代理方法自动调用):
开始编辑时调用
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
结束编辑时调用
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
文字内容改变时调用此方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
点击清除按钮时调用
- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
点击return键时调用
- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
专门为输入框准备的响应事件
UIControlEventEditingDidBegin = 1 << 16, // UITextField
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing
注意:1.如果想使用代理方法,必须先导入代理
2.如果代理方法没有触发,看是否挂上了代理
3.使用左右侧视图时要配合左右侧视图样式使用
leftView:输入框左侧视图
rightView:输入框右侧视图
❤️❤️不是设置了左右侧视图就可以显示,需要配合使用以下属性❤️❤️
leftViewMode:设置什么情况下显示左侧视图的
rightViewMode:设置什么情况下显示右侧视图的
*/
//1.导入代理的名字
//2.挂上代理,没触发代理方法表示没有挂上代理
//3.实现代理的方法
@interface AppDelegate ()<UITextFieldDelegate>//❤️❤️<UITextFieldDelegate>1.导入代理的名字❤️❤️
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window makeKeyAndVisible];
UITextField *accTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 40, CGRectGetWidth([UIScreen mainScreen].bounds)-20,40)];
// accTextField.backgroundColor = [UIColor cyanColor];
// 提示文字
accTextField.placeholder = @"请输入账号";
//设置输入框的样式
accTextField.borderStyle = UITextBorderStyleBezel;
// 清除按钮,具有一键清除输入框中内容的功能
accTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//
UIImageView *leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
// 可直接向图片视图中添加图片
leftImageView.image = [UIImage imageNamed:@"play48.png"];
// 在输入框左侧设置视图
accTextField.leftView = leftImageView;
accTextField.leftViewMode = UITextFieldViewModeAlways;
// 键盘的样式
accTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
UIView *inputV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 40)];
inputV.backgroundColor = [UIColor greenColor];
// 输入时弹出inputV 框
// accTextField.inputView = inputV;
accTextField.inputAccessoryView = inputV;
// ❤️❤️2.想使用代理方法,必须挂上代理❤️❤️
accTextField.delegate = self;//⭐️⭐️⭐️很重要⭐️⭐️⭐️
[self.window addSubview:leftImageView];
[self.window addSubview:accTextField];
return YES;
}
//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
//- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
//- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
//- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
//
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
//
//- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"开始编辑");
}
//当点击模拟器键盘上的 return键时 自动触发
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
// 回收键盘,标志着输入结束
[textField resignFirstResponder];
return YES;
}
//编辑结束的时候调用,return 和 回车 都可调用
- (void)textFieldDidEndEditing:(UITextField *)textField{
// 此属性可以获得输入框的内容
NSLog(@"%@",textField.text);
}
//编辑结束的时候调用,与上面方法等价
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
// 此属性可以获得输入框的内容
NSLog(@"%@",textField.text);
return YES;
}
//点击清除按钮时调用
- (BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"点清除按钮时");
return YES;
}
//可以获得用户每输入一次的 文字 及其 位置
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSLog(@"%@",string);
return YES;
}