• iOS UITextField输入框


    参考:https://www.jianshu.com/p/f310fa9459ed   iOS 键盘回车键(换行、回车符)修改

             https://www.jianshu.com/p/532d12375e9a     iOS中收起键盘的几种方式

          

    大体来说,收起键盘的方法有:

    • 让textFiled放弃成为第一响应者(resignFirstResponder)
    • 让view或者子view强制结束编辑状态(endEditing)

    1.输入完以后用户点击return后收起键盘。

    可以点击return收起键盘并不意味着弹出来的键盘一定都有return这个按键,比如数字键盘类型就没有,UIKeyboardTypeWebSearch类型的键盘return键就变成了Go键,需要注意的是这时候的Go键相当于return键。

    要想实现点击return让键盘收起,首先需要设置textField代理,当然还有遵循UITextFieldDelegate协议:

    _textField.delegate = self;
    

    然后实现协议方法- (BOOL)textFieldShouldReturn:(UITextField *)textField;,当然你也可以在这个方法里 main实现一些需求逻辑,比如判断输入的内容符不符合要求等。

    - (BOOL)textFieldShouldReturn:(UITextField *)textField{
        return [textField resignFirstResponder];
    }
    

    2.点击背景后收起键盘

    点击背景收起键盘可以实现view的- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event方法,然后执行view的- (BOOL)endEditing:(BOOL)force;方法。

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        [self.view endEditing:YES]; //实现该方法是需要注意view需要是继承UIControl而来的
    }
    

    还有一些比较笨的方法也可以实现达到点击背景后收起键盘的效果,比如:在背景view上面加手势,添加UIButton或者view,然后在对应的点击事件的监听方法中实现resignFirstResponder或者endEditing

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];  
    //设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。  
    tapGestureRecognizer.cancelsTouchesInView = NO;  
    //将触摸事件添加到view上
    [self.view addGestureRecognizer:tapGestureRecognizer];   
    
    -(void)keyboardHide:(UITapGestureRecognizer*)tap{  
        [textFiled resignFirstResponder];  
    } 
    

    3.不用view实现endEditing的方法

    如果说在比较难获取viewController的view时,可以使用下面的方法:

    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    

    或者

    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];


    UITextField 和 UITextView

    如图修改键盘

    UIReturnKeyDefault

     
    UIReturnKeyDefault.jpg

    UIReturnKeyGo

     
    UIReturnKeyGo.jpg

    UIReturnKeyGoogle

     
    UIReturnKeyGoogle.jpg

    UIReturnKeyJoin

     
    UIReturnKeyJoin.jpg

    UIReturnKeyNext

     
    UIReturnKeyNext.jpg

    UIReturnKeyRoute

     
    UIReturnKeyRoute.jpg

    UIReturnKeySearch

     
    UIReturnKeySearch.jpg

    UIReturnKeySend

     
    UIReturnKeySend.jpg

    UIReturnKeyYahoo

     
    UIReturnKeyYahoo.jpg

    UIReturnKeyDone

     
    UIReturnKeyDone.jpg

    UIReturnKeyEmergencyCall

     
    UIReturnKeyEmergencyCall.jpg

    UIReturnKeyContinue

     
    UIReturnKeyContinue.jpg

    UITextView *textView = [[UITextView alloc] init];
    textView.returnKeyType = UIReturnKeyDone;
    
    UITextField *textField= [[UITextField alloc] init];
    textView.returnKeyType = UIReturnKeyDone;
    

    对回车符事件进行监听

    实现 UITextViewDelegate代理里面响

    • -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

    实现 UITextFieldDelegate代理里面响

    • -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    
    这个函数的最后一个参数text代表你每次输入的的那个字,所以:
    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
    {
        if ([text isEqualToString:@"
    "]){ //判断输入的字是否是回车,即按下return
            //[textView endEditing:YES];
            //在这里做你响应return键的代码
    
            return NO; //这里返回NO,就代表return键值失效,即页面上按下return,不会出现换行,如果为yes,则输入页面会换行
        }
    
        return YES;
    }
    
    typedef NS_ENUM(NSInteger, UIReturnKeyType) {
        UIReturnKeyDefault,
        UIReturnKeyGo,
        UIReturnKeyGoogle,
        UIReturnKeyJoin,
        UIReturnKeyNext,
        UIReturnKeyRoute,
        UIReturnKeySearch,
        UIReturnKeySend,
        UIReturnKeyYahoo,
        UIReturnKeyDone,
        UIReturnKeyEmergencyCall,
        UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),
    };

    //
    //  Subtitle_I_loveVC.m
    //  meiritupian001
    //
    //  Created by FAMAR-YF-04 on 2020/6/23.
    //  Copyright © 2020 FAMAR-YF-04. All rights reserved.
    //
    
    #import "Subtitle_I_loveVC.h"
    #import "send_Subtitle.h"
    
    @interface Subtitle_I_loveVC ()<UITextFieldDelegate>
    
    @property (weak, nonatomic) IBOutlet UITextField *input_Sentence;
    @property (nonatomic, strong) send_Subtitle *send_SubtitleVC;
    @property (nonatomic, weak)NSString *str_input;//输入的数据
    @end
    
    @implementation Subtitle_I_loveVC
    
    -(void)viewWillAppear:(BOOL)animated{
    
       NSLog(@"视图即将加入窗口时");
        [super viewDidAppear:animated];
           
           if([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
               
               SEL selector = NSSelectorFromString(@"setOrientation:");
               NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
               [invocation setSelector:selector];
               [invocation setTarget:[UIDevice currentDevice]];
               
               NSInteger val = UIInterfaceOrientationPortrait;//横屏
               [invocation setArgument:&val atIndex:2];
               [invocation invoke];
               
           }
      
    }
    
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
        self.input_Sentence.delegate = self;
        self.input_Sentence.returnKeyType = UIReturnKeyDone;//键盘类型
    }
    
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField{
        return [textField resignFirstResponder];
    }
    
    // 点击空白处收起键盘
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        [self.view endEditing:YES]; //实现该方法是需要注意view需要是继承UIControl而来的
    }
    
    - (IBAction)getinputstr:(id)sender {
        
        UITextField *_field = (UITextField *)sender;
        NSLog(@"fhfhff:%@",[_field text]);
        self.str_input = [_field text];
        
    }
    
    - (IBAction)pushSend_Subtitle:(id)sender {
        _send_SubtitleVC = [[send_Subtitle alloc]init];
        if(self.input_Sentence.text.length == 0){
            NSLog(@"输入框输入为空");
            NSLog(@"输入框输入为空:%@",self.input_Sentence.placeholder);
            self.send_SubtitleVC.str_subtitles = self.input_Sentence.placeholder;
        }else{
            NSLog(@"输入框输入不为空");
             NSLog(@"输入框输入不为空:%@",[self.input_Sentence text] );
            self.send_SubtitleVC.str_subtitles = [self.input_Sentence text];
        }
        [self.navigationController pushViewController:self.send_SubtitleVC animated:NO];
    //    [self presentViewController:self.send_SubtitleVC animated:YES completion:nil];
    }
    
    
    @end
  • 相关阅读:
    Linux的CPU负载
    C++ 内接连与外接连
    boost 串口通信
    创建型模式--单例模式
    Python urllib与urllib2
    CodeBlocks使用boost+MinGW
    Python 线程(七):local(线程局部存储)
    Python 线程(六):Timer(定时器)
    Python 线程(五):Event
    Python 线程(四):Semphore同步
  • 原文地址:https://www.cnblogs.com/gaozhang12345/p/13254983.html
Copyright © 2020-2023  润新知