UITextField文本框

2020-10-05  本文已影响0人  我不白先生

UITextField文本框

接收用户输入的内容

placeHolder提示性文字如:
self.textField1.placeholder = @"请输入用户名";
borderStyle(RoundedRect)边框样式

/*UITextBorderStyleNone,
    UITextBorderStyleLine,//黑线框
    UITextBorderStyleBezel,//黑线框 带阴影
    UITextBorderStyleRoundedRect圆角矩形*/
    self.textField1.borderStyle = UITextBorderStyleNone;

background(UIImage)背景图片
self.textField1.background = [UIImage imageNamed:@"BTN"];

  1. 方法一:只要让文本框失去焦点即可。失去焦点也叫放弃第一响应者身份
    [text resignFirstResponder];
    2.方法二:如果界面中文本框过多,希望批量关闭所有文本框弹出的键盘。则设置文本框的父视图结束编辑状态即可
    //结束 textField1的第一响应者身份
    [self.textField1 resignFirstResponder];
    //结束 textField2的第一响应者身份
    //从self.view 取出 textField 这个子视图
    UITextField *tf2 = [self.view viewWithTag:100];
    [tf2 resignFirstResponder];
    //结束父视图的编辑状态 一旦结束了父视图的编辑状态,无论它里面的子视图无论谁在获取焦点,全部都取消
    [self.view endEditing:YES];
@interface MyViewController ()
@property(nonatomic,strong)UITextField *tf1;
@property(nonatomic,strong)UITextField *tf2;
@end
@implementation MyViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //如果编辑第一个 文本框 让 第二个文本框获取第一响应身份
     if(self.tf1.isFirstResponder)
   {
      [self.tf2 becomeFirstResponder];
       return;
   }
    //如果编辑第二个 文本框 让 第二个文本框获取第一响应身份
    if(self.tf2.isFirstResponder)
    {
        [self.tf2 becomeFirstResponder];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tf1 = [[UITextField alloc]init];
    self.tf1.frame = CGRectMake(50, 200, 200, 50);
    self.tf1.placeholder = @"请输入用户名";
    //UIControlEventEditingDidEndOnExit 文件输入框点击return键的事件
    [self.tf1 addTarget:self action:@selector(textFieldClickReturn:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [self.view addSubview:self.tf1];
    self.tf2 = [[UITextField alloc]init];
    self.tf2.frame = CGRectMake(50, 300, 200, 50);
    self.tf2.placeholder = @"请输入用户名";
    [self.tf2 addTarget:self action:@selector(textFieldClickReturn:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [self.view addSubview:self.tf2];
}
//如果UIControlEventEditingDidEndOnExit 事件方法一旦实现,不用写任何代码键盘都会回收
-(void)textFieldClickReturn:(UITextField*)  sender{
    if(self.tf1 == sender)
    [self.tf2 becomeFirstResponder];
}
@interface MyViewController ()
@property(nonatomic,assign)NSInteger y;
@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.y = 20;
    self.view.backgroundColor = [UIColor whiteColor];
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(20, self.y, self.view.frame.size.width - 40, 40)];
    textField.placeholder = @"请输入内容";
    textField.borderStyle = UITextBorderStyleLine;
    //添加点击return事件方法
    [textField addTarget:self action:@selector(addLabel:) forControlEvents:UIControlEventEditingDidEndOnExit];
    
    [self.view addSubview:textField];
    self.y += textField.frame.size.height +20;
}
-(void)addLabel:(UITextField*) sender
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, self.y, self.view.frame.size.width, 40)];
    label.text = sender.text;
    label.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:label];
    self.y += label.frame.size.height +20;
    //清空输入框中内容
    sender.text = @"";
}
上一篇下一篇

猜你喜欢

热点阅读