您的位置:首页 > 产品设计 > UI/UE

TextField

2015-12-07 20:19 281 查看
#import "AppDelegate.h"

//要使用输入框的代理方法 1.导入代理名字2.挂上代理(如果代理方法不触发
说明没有挂上代理)3.实现代理的方法()

//1⃣️导入代理名字

@interface
AppDelegate ()<UITextFieldDelegate>//导入代理的名字

@end

@implementation AppDelegate

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

/*

UITextField:-->UIControl的子类-->UIView的子类

 UITextField:文本输入框

 enabled

 text:可以获得
或者 改变输入框的文字内容

 placeholder:提示文字

 textColor字体颜色:

 font:字号

 textAligement:对齐方式

 adjustsFontSizeToFitWidth:让文字自动根据宽度适配

 minimumFontSize:设置
文字的最小字号

 delegate:代理
让别人帮他做某件事

 

 clearButtonMode:设置清除按钮什么时候显示

 UITextFieldViewModeNever,永远都不显示

 UITextFieldViewModeWhileEditing,当编辑的时候显示

 UITextFieldViewModeUnlessEditing,不再编辑的时候显示

 UITextFieldViewModeAlways,永远显示

 

 

 

 

  leftView:左侧视图--》输入框左侧视图

 rightView:右侧视图--》输入框右侧视图

*****并不是设置了左侧右侧视图就可以显示出来
需要配合使用下面属性****

leftViewMode:设置什么情况下
显示左侧视图

rightViewMode:设置什么情况下
显示右侧视图

 

 leftViewMode、rightViewMode的使用状态:

 UITextFieldViewModeNever,永远都不显示

 UITextFieldViewModeWhileEditing,当编辑的时候显示

 UITextFieldViewModeUnlessEditing,不再编辑的时候显示

 UITextFieldViewModeAlways,永远显示

 

 inputView:键盘上面的视图

 inputAccessoryView:键盘区域的视图

 

 borderStyle:输入框的样式

 UITextBorderStyleNone:没有样式

 UITextBorderStyleLine:黑线边框

 UITextBorderStyleBezel:黑线阴影

 UITextBorderStyleRoundedRect:圆角

 

 输入框的方法:代理方法

 1.开始编辑的时候调用

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        // return NO to disallow editing.

 - (void)textFieldDidBeginEditing:(UITextField *)textField;           // became first responder

2.结束编辑的时候调用

 - (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

3.文字内容发生改变的时候调用

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

4.点击清除按钮的时候调用

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

5.点击回车键的时候调用

 - (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

 

 键盘的样式(KeyboardType):

 UIKeyboardTypeDefault,    

 UIKeyboardTypeASCIICapable,         

 UIKeyboardTypeNumbersAndPunctuation,

 UIKeyboardTypeNumberPad, 
只显示数字0~9

 UIKeyboardTypePhonePad,电话类型(1~9、*,0#)

 UIKeyboardTypeNamePhonePad,

 UIKeyboardTypeEmailAddress,
邮箱地址类型

 UIKeyboardTypeDecimalPad ,

 UIKeyboardTypeTwitter ,

 UIKeyboardTypeWebSearch ,
网页搜索

 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,

 

 

 

 

 

 

 

 注意点:

 1.如果想使用代理方法
必须先导入代理

 2.如果代理方法没有触发
看是否 挂上了代理

 3.在使用左右侧视图的时候
要配合左右侧视图的Model使用

*/

    

    [self.window
makeKeyAndVisible];

#pragma mark---初始化一个文本输入框----

    UITextField *TextField=[[UITextField
alloc]initWithFrame:CGRectMake(10,
40,CGRectGetWidth([UIScreen
mainScreen].bounds)-20,40)];

    TextField.borderStyle=UITextBorderStyleRoundedRect;//设置输入框的样式

    TextField.placeholder=@"请输入账号";//文本框的提示文字

    TextField.clearButtonMode=UITextFieldViewModeWhileEditing;//设置文本框的清除按钮状态

#pragma mark---给文本输入框添加一个带图片的左侧视图----

    UIImageView *leftimageView=[[UIImageView
alloc]initWithFrame:CGRectMake(0,
0, 30,
30)];

    UIImage *image=[UIImage
imageNamed:@"0"];

    leftimageView.image=image;

   // leftimageView.backgroundColor=[UIColor redColor];

    

    TextField.leftView=leftimageView;

    TextField.leftViewMode=UITextFieldViewModeAlways;//这两个一定要配合使用

#pragma mark---给文本输入框的键盘添加一个视图----

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

    inputV.backgroundColor=[UIColor
orangeColor];

//    TextField.inputView=inputV;//添加键盘上面的视图

    TextField.inputAccessoryView=inputV;//添加键盘区域的视图

    TextField.keyboardAppearance=UIKeyboardTypeWebSearch ;

 //想使用代理方法
必须先挂上代理

    TextField.delegate=self;

    

    

    

//2⃣️挂上代理

    [self.window
addSubview:TextField];

    

//    - (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)

    

//- (BOOL)textFieldShouldReturn:(UITextField *)textField

    

//    - (void)textFieldDidEndEditing:(UITextField *)textField

         return YES;

}

#pragma mark ----3⃣️实现代理------

//点击return键的时候触发

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];//回收键盘--》标志着输入结束

    

//    NSLog(@"调用");

    return
YES;

}

//编辑结束的时候调用 
下面两种方法一样

- (void)textFieldDidEndEditing:(UITextField *)textField{

NSLog(@"%@",textField.text);

}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{

    return
YES;

}

//开始编辑的时候调用 
下面两种方法一样

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    return
YES;

}

- (void)textFieldDidBeginEditing:(UITextField *)textField{

    NSLog(@"开始编辑");

}

//可以获得用户每输入一次的文字、位置

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    NSLog(@"%@",string);

    return
YES;

}

//点击清除的时候调用

- (BOOL)textFieldShouldClear:(UITextField *)textField{

    NSLog(@"点击清除按钮");

    return
YES;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UI