OC-UI阶段学习04-UITextField文本输入框

UITextField--文本框


UITextField是控制文本输入和显示的控件,只能输入单行

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

#pragma mark UITextField文本框,单行
    
    //初始化
    UITextField *myTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 350, 150, 20)];

    //占位字符,没有输入时显示的文本,点击文本框时自动清空
    myTextField.placeholder = @"请输入手机号";

    //设置键盘样式,数字键盘,字母键盘等
    myTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    //设置边框样式
    myTextField.borderStyle = UITextBorderStyleRoundedRect;

    //设置密码样式,使输入内容隐藏
    myTextField.secureTextEntry = NO;
    [self.window addSubview:Line];
    
    //设置右侧小叉号,点击可以删去所有输入内容
    myTextField.clearButtonMode = UITextFieldViewModeAlways;
    //设置首字母大写
    myTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    
    //通过属性控制是否可以编辑
    myTextField.enabled = YES;

    //自定义键盘
    UIView *keyBoardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), 256)];
   
    //定义一个button放在键盘上
    UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [firstButton setTitle:@"1" forState:UIControlStateNormal];
    firstButton.frame = CGRectMake(10, 10, 50, 50);
    [keyBoardView addSubview:firstButton];
    firstButton.backgroundColor = [UIColor whiteColor];
    //设置点击效果
    [firstButton addTarget:self action:@selector(keyButton:) forControlEvents:UIControlEventTouchUpInside];
    
    myTextField.inputView = keyBoardView;
    
    //显示
    [self.window addSubview:myTextField];
    
    //新建一个输入框,设置代理方法
    UITextField *delegateTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 200, 100, 100)];
    delegateTextField.borderStyle = UITextBorderStyleLine;
    delegateTextField.placeholder = @"代理方法";
    
    delegateTextField.keyboardType = UIKeyboardTypeDefault;
    //设置代理方法,代理人是本类
    delegateTextField.delegate = self;
    [self.window addSubview:delegateTextField];

    return YES;
}

//自定义按钮的
-(void)keyButton:(UIButton*)sender
{
    sender.selected = !sender.selected;
    UITextField *field = (UITextField*)[self.window viewWithTag:1001];
    NSString *title = [sender titleForState:UIControlStateNormal];
    field.text = [field.text stringByAppendingString:title];

    //回收键盘 取消第一响应者 结束编辑状态
    [field resignFirstResponder];

    //另一种方法回收键盘
    //[self.view endEditing YES];

    //变为第一响应者
    [field becomeFirstResponder];
 }

#pragma makr 代理方法

//是否可以开始编辑状态
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    NSLog(@"%s",__func__);
    return YES;
}

//已经进入编辑状态
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"%s",__func__);
}
//是否可以结束编辑
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"%s",__func__);
    return YES;
}
//已经结束编辑状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"%s",__func__);
}
//点击右下角return按钮所触发的代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"return");
    //在此回收键盘
    [textField resignFirstResponder];
    return YES;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //可以限制文字的改变
    return YES;
} 

设置了代理方法后,UITextField的各种状态会触发代理方法,在本例中代理人设置成了self,也就是本类,然后在本类中实习代理协议要求的方法,这些方法都是@optional的,可以选择性实现

你可能感兴趣的:(UI,oc,UITextField,文本输入框,uicontrol)