UITextField用法

来源:互联网 发布:网络大电影 铁扇公主 编辑:程序博客网 时间:2024/06/05 15:57

#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 :数字和符号

 UIKeyboardTypeURLURL网址类型

 

 下面三条iPad专用

 UIKeyboardTypeNumberPad:只显示 0 - 9的数字            UIKeyboardTypePhonePad:电话类型(1-9*0#

 UIKeyboardTypeNamePhonePad

 

 UIKeyboardTypeEmailAddress:邮箱地址类型

 UIKeyboardTypeDecimalPadNS_ENUM_AVAILABLE_IOS(4_1),    UIKeyboardTypeTwitterNS_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 firstresponder status. NO to disallow the editing session to end

 -(void)textFieldDidEndEditing:(UITextField *)textField;            // may be called if forced even if shouldEndEditingreturns NO (e.g. view removed from window) or endEditing:YES called

 文字内容改变时调用此方法

 -(BOOL)textField:(UITextField *)textFieldshouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string;   // return NO to not change text

 点击清除按钮时调用

 -(BOOL)textFieldShouldClear:(UITextField *)textField;              // called when clear button pressed. return NO toignore (no notifications)

 点击return键时调用

 -(BOOL)textFieldShouldReturn:(UITextField *)textField;             // called when 'return' key pressed. return NO toignore.

 

 专门为输入框准备的响应事件

 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.实现代理的方法

@interfaceAppDelegate ()<UITextFieldDelegate>//❤️❤️<UITextFieldDelegate>1.导入代理的名字❤️❤️

 

@end

 

@implementationAppDelegate

 

 

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

   

    [self.windowmakeKeyAndVisible];

    

    UITextField *accTextField = [[UITextFieldalloc]initWithFrame:CGRectMake(10,40, CGRectGetWidth([UIScreenmainScreen].bounds)-20,40)];

//   accTextField.backgroundColor = [UIColor cyanColor];

//   提示文字

   accTextField.placeholder =@"请输入账号";

//设置输入框的样式

    accTextField.borderStyle =UITextBorderStyleBezel;

//   清除按钮,具有一键清除输入框中内容的功能

    accTextField.clearButtonMode =UITextFieldViewModeWhileEditing;

 

//

    UIImageView *leftImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, 30, 30)];

//   可直接向图片视图中添加图片

   leftImageView.image = [UIImageimageNamed:@"play48.png"];

//   在输入框左侧设置视图

   accTextField.leftView = leftImageView;

    accTextField.leftViewMode =UITextFieldViewModeAlways;

//   键盘的样式

    accTextField.keyboardType =UIKeyboardTypeNumbersAndPunctuation;

    

    UIView *inputV = [[UIView alloc]initWithFrame:CGRectMake(0,0, CGRectGetWidth([UIScreenmainScreen].bounds),40)];

    inputV.backgroundColor = [UIColorgreenColor];

//   输入时弹出inputV

//   accTextField.inputView = inputV;

   accTextField.inputAccessoryView = inputV;

    

//   ❤️❤️2.想使用代理方法,必须挂上代理❤️❤️

   accTextField.delegate =self;//⭐️⭐️⭐️很重要⭐️⭐️⭐️

    

 

    [self.windowaddSubview:leftImageView];

     [self.windowaddSubview: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 firstresponder status. NO to disallow the editing session to end

//-(void)textFieldDidEndEditing:(UITextField *)textField;            // may be called if forced even if shouldEndEditingreturns NO (e.g. view removed from window) or endEditing:YES called

//

//-(BOOL)textField:(UITextField *)textFieldshouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string;   // return NO to not change text

//

//-(BOOL)textFieldShouldClear:(UITextField *)textField;              // called when clear button pressed. return NO toignore (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)rangereplacementString:(NSString *)string{

    

    NSLog(@"%@",string);

    return YES;

}


0 0
原创粉丝点击