(Swift&OC)UItextViewの使い方

13689 ワード

一、キーワード
  • 1.1.弾性方向alwaysBounceVertical垂直alwaysBounceHorizontal水平
  • 1.2.UITExtViewキーボードをスライドしてkeyboardDismissMode=UIscrollViewKeyboardDismissMode.onDrag
  • 1.3.入力ボックスにコンテンツがあるかどうかhasText:ステータスはtruefalse
  • である.
  • 1.4. UItextViewにview inputAccessoryView
  • を追加
  • 1.5. カスタムキーボードinputView
  • 1.6.キーボードのお知らせ(アニメーションのリズム設定)については、次のコードコメントをご覧ください.
  • UIViewアニメーションの本質はコアアニメーションなので、コアアニメーションにアニメーションのリズムを設定することができます

  • 二、Swiftでの使用
  • 2.1.キーボードにview(JKTextViewControl)
    import UIKit
    
    class JKTextViewController: UIViewController {
    
    override func viewDidLoad() {
      super.viewDidLoad()
      
      /**
          1.        
       */
      title = "JKTextView"
      view.backgroundColor = UIColor.white
      self.edgesForExtendedLayout = []
      
      /**
          2.  UITextView           view
       */
      view.addSubview(textView)
      textView.addSubview(placeholderLabel)
      textView.inputAccessoryView = jkInputAccessoryView
    }
    
    // MARK: UITextView    
    private lazy var textView:UITextView = {
      
      let textview = UITextView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-64))
      textview.font = UIFont.systemFont(ofSize: 20)
      //          
      textview.alwaysBounceVertical = true
      //           
      textview.keyboardDismissMode = UIScrollViewKeyboardDismissMode.onDrag
      return textview
    }()
    
    // MARK: UILabel     
    private lazy var placeholderLabel: UILabel = {
      
      let label = UILabel(frame: CGRect(x: 5, y: 9, width: UIScreen.main.bounds.width-10, height: 22))
      label.font = UIFont .systemFont(ofSize: 20)
      label.textColor = UIColor.red
      label.text = "     "
      return label
      
    }()
    
     // MARK: UIView      view
     private lazy var jkInputAccessoryView: UIView = {
      
       let inputView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
       inputView.backgroundColor = UIColor.green
       return inputView
      
     }()
    }
    
    // MARK: UITextView     
     extension JKTextViewController: UITextViewDelegate{
    
     func textViewDidChange(_ textView: UITextView) {
      
       /**
           hasText     UITextView     
        */
       placeholderLabel.isHidden = textView.hasText
    
       }
     }
    
  • を追加
  • 2.2.カスタムキーボード(JKTextViewControl)
    import UIKit
    
    class JKTextViewController: UIViewController {
    
    override func viewDidLoad() {
      super.viewDidLoad()
      
      // 0.             
      NotificationCenter.default.addObserver(self, selector: #selector(JKTextViewController.keyboardChange), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
      
      /**
          1.        
       */
      title = "JKTextView"
      view.backgroundColor = UIColor.white
      self.edgesForExtendedLayout = []
      
      /**
          2.  UITextView           view
       */
      view.addSubview(textView)
      textView.addSubview(placeholderLabel)
      /**
          3.          view
       */
      view.addSubview(toolBar)
      toolBar.addSubview(toolBarBtn)
      
    }
    
     /**
                   
      */
     @objc func keyboardChange(notify: NSNotification)
     {
      // 1.       rect
      let value = notify.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
      let rect = value.cgRectValue
      
      // 2.        
      //    : Y = 409 height = 258
      //    : Y = 667 height = 258
      // 667 - 409 = 258
      // 667 - 667 = 0
      
      let duration = notify.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
      /*
                      ,              (  ) 7
       7 apple API         ,         
       7         :           ,             ,          
                    
       
                 7,                   0.5
       
       UIView          ,                
       */
      // 1.         
      let curve = notify.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! NSNumber
      
      UIView.animate(withDuration: duration.doubleValue) { () -> Void in
          
          // 2.      
          UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: curve.intValue)!)
          self.toolBar.frame.origin.y = rect.origin.y - 64 - 44
      }
      
      let anim = toolBar.layer.animation(forKey: "position")
      print("duration = \(String(describing: anim?.duration))")
    }
    
    // MARK:     
    @objc func switchKeyBoard() {
      
      print("      ")
      print(#function)
      //   :           ,   inputView = nil
      //                 ,   inputView != nil
      //        print(textView.inputView)
      
      // 1.    
      textView.resignFirstResponder()
      
      // 2.  inputView
      textView.inputView = (textView.inputView == nil) ? keyBoardView : nil
      
      // 3.       
      textView.becomeFirstResponder()
    }
    
     deinit
     {
       NotificationCenter.default.removeObserver(self)
     }
    
     // MARK: UITextView    
     private lazy var textView:UITextView = {
      
      let textview = UITextView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-64))
      textview.font = UIFont.systemFont(ofSize: 20)
      //          
      textview.alwaysBounceVertical = true
      //           
      textview.keyboardDismissMode = UIScrollViewKeyboardDismissMode.onDrag
      return textview
     }()
    
     // MARK: UILabel     
     private lazy var placeholderLabel: UILabel = {
      
      let label = UILabel(frame: CGRect(x: 5, y: 9, width: UIScreen.main.bounds.width-10, height: 22))
      label.font = UIFont .systemFont(ofSize: 20)
      label.textColor = UIColor.red
      label.text = "     "
      return label
      
     }()
    
     // MARK: UIView      view
     private lazy var toolBar: UIView = {
      
      let inputView = UIView(frame: CGRect(x: 0, y: UIScreen.main.bounds.height-64-44, width: UIScreen.main.bounds.width, height: 44))
      inputView.backgroundColor = UIColor.green
      return inputView
      
     }()
    
     // MARK: UIView      view     button
     private lazy var toolBarBtn: UIButton = {
      
      let btn = UIButton(frame: CGRect(x: UIScreen.main.bounds.width-150, y: 0, width: 100, height: 44))
      btn.backgroundColor = UIColor.purple
      btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
      btn.setTitle("    ", for: UIControlState.normal)
      btn.addTarget(self, action: #selector(JKTextViewController.switchKeyBoard), for: UIControlEvents.touchUpInside)
      return btn
      
     }()
    
     // MARK:      
     private lazy var keyBoardView: UIView = {
      
      let keyboardView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100))
      keyboardView.backgroundColor = UIColor.brown
      return keyboardView
      
      }()
    }
    
    // MARK: UITextView     
    extension JKTextViewController: UITextViewDelegate{
    
      func textViewDidChange(_ textView: UITextView) {
        /**
            hasText     UITextView     
         */
         placeholderLabel.isHidden = textView.hasText
      }
    }
    
  • を切り替えます.
    三、OCでの使用
  • 3.1.キーボードにview(TestView Control)
    #import "TestViewController.h"
    #define CIO_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
    #define CIO_SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
    #define TitleBlackCOLOR [UIColor colorWithRed:(51)/255.0 green:(51)/255.0 blue:(51)/255.0 alpha:1]
    #define backGayCOLOR [UIColor colorWithRed:(232)/255.0 green:(232)/255.0 blue:(232)/255.0 alpha:1]
    @interface TestViewController ()
    
    /**
       UITextView      
     */
    @property(nonatomic,strong) UITextView *jkTextView;
    /**
         
    */
    @property(nonatomic,strong) UILabel *placeholderLabel;
    /**
             view
     */
    @property(nonatomic,strong) UIView *jkInputAccessoryView;
    
    @end
    
    @implementation TestViewController
    
    - (void)viewDidLoad {
       [super viewDidLoad];
    
       self.edgesForExtendedLayout = UIRectEdgeNone;
       self.view.backgroundColor = [UIColor whiteColor];
    
       [self.view addSubview:self.jkTextView];
       [self.jkTextView addSubview:self.placeholderLabel];
       self.jkTextView.inputAccessoryView = self.jkInputAccessoryView;
    
    }
    
    #pragma mark  textview   
    -(void)textViewDidChange:(UITextView *)textView{
    
       self.placeholderLabel.hidden = textView.hasText;
    }
    
    -(UITextView *)jkTextView{
    
      if (!_jkTextView) {
    
        _jkTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, CIO_SCREEN_WIDTH,CIO_SCREEN_HEIGHT-64)];
        [_jkTextView becomeFirstResponder];
        // UITextView          
        _jkTextView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _jkTextView.alwaysBounceVertical = YES;
        _jkTextView.delegate = self;
        _jkTextView.backgroundColor = [UIColor whiteColor];
        _jkTextView.textColor = TitleBlackCOLOR;
        _jkTextView.font = [UIFont systemFontOfSize:16.f];
       }
      return _jkTextView;
    }
    
    -(UILabel *)placeholderLabel{
    
        if (!_placeholderLabel) {
    
           _placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 9, CIO_SCREEN_WIDTH-10, 22)];
           _placeholderLabel.text = @"     ";
           _placeholderLabel.textColor = backGayCOLOR;
           _placeholderLabel.font = [UIFont systemFontOfSize:20];
         }
    
      return _placeholderLabel;
    }
    
    -(UIView *)jkInputAccessoryView{
    
       if (!_jkInputAccessoryView) {
    
          _jkInputAccessoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CIO_SCREEN_WIDTH, 44)];
          _jkInputAccessoryView.backgroundColor = [UIColor redColor];
        }
       return _jkInputAccessoryView;
     }
    
    @end
    
  • を追加
  • 3.2.カスタムキーボード(TestView Control)
    #import "TestViewController.h"
    #import "UIView+JKUiviewExtension.h"
    
    #define CIO_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
    #define CIO_SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
    #define TitleBlackCOLOR [UIColor colorWithRed:(51)/255.0 green:(51)/255.0 blue:(51)/255.0 alpha:1]
    #define backGayCOLOR [UIColor colorWithRed:(232)/255.0 green:(232)/255.0 blue:(232)/255.0 alpha:1]
    
    @interface TestViewController ()
    
     /**
         UITextView      
       */
       @property(nonatomic,strong) UITextView *jkTextView;
       /**
              
        */
       @property(nonatomic,strong) UILabel *placeholderLabel;
       /**
                  view
        */
        @property(nonatomic,strong) UIView *tooBarView;
        @property(nonatomic,strong) UIButton *tooBarViewBtn;
    
    //      
    @property(nonatomic,strong) UIView *keyBoardView;
    
    @end
    
    @implementation TestViewController
    
    - (void)viewDidLoad {
      [super viewDidLoad];
    
      self.edgesForExtendedLayout = UIRectEdgeNone;
      self.view.backgroundColor = [UIColor whiteColor];
    
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
      [self.view addSubview:self.jkTextView];
      [self.jkTextView addSubview:self.placeholderLabel];
      [self.view addSubview: self.tooBarView];
      [self.tooBarView addSubview:self.tooBarViewBtn];
    }
    
     #pragma mark     
     -(void)switchKeyBoard{
    
       // 1.    
       [self.jkTextView resignFirstResponder];
    
       // 2.  inputView
       self.jkTextView.inputView = (self.jkTextView.inputView == nil) ? self.keyBoardView : nil;
    
      // 3.       
      [self.jkTextView becomeFirstResponder];
     }
    
     #pragma mark     
     //           
     -(void)keyBoardChange:(NSNotification *)notification
     {
       //          frame
       CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
       //          
      float duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
      NSLog(@"        =%lf",duration);
    
      /*
                       ,              (  ) 7
        7 apple API         ,         
        7         :           ,             ,          
                     
    
                  7,                   0.5
    
        UIView          ,                
      */
     // 1.         
     float curve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] floatValue];
    
     [UIView animateWithDuration:duration animations:^{
      
      // 2.      
      [UIView setAnimationCurve:curve];
      
      self.tooBarView.y = keyboardFrame.origin.y - 64 - 44;
      
     }];
    }
    
    #pragma mark  textview   
    -(void)textViewDidChange:(UITextView *)textView{
    
        self.placeholderLabel.hidden = textView.hasText;
    }
    
    -(UITextView *)jkTextView{
    
       if (!_jkTextView) {
      
      _jkTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, CIO_SCREEN_WIDTH,CIO_SCREEN_HEIGHT-64)];
      [_jkTextView becomeFirstResponder];
      // UITextView          
      _jkTextView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
      _jkTextView.alwaysBounceVertical = YES;
      _jkTextView.delegate = self;
      _jkTextView.backgroundColor = [UIColor whiteColor];
      _jkTextView.textColor = TitleBlackCOLOR;
      _jkTextView.font = [UIFont systemFontOfSize:16.f];
    }
    
     return _jkTextView;
    }
    
    -(UILabel *)placeholderLabel{
    
       if (!_placeholderLabel) {
      
       _placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 9, CIO_SCREEN_WIDTH-10, 22)];
       _placeholderLabel.text = @"     ";
       _placeholderLabel.textColor = backGayCOLOR;
       _placeholderLabel.font = [UIFont systemFontOfSize:20];
      }
    
     return _placeholderLabel;
    }
    
    -(UIView *)tooBarView{
    
       if (!_tooBarView) {
    
         _tooBarView = [[UIView alloc]initWithFrame:CGRectMake(0, CIO_SCREEN_HEIGHT-64-44, CIO_SCREEN_WIDTH, 44)];
         _tooBarView.backgroundColor = [UIColor redColor];
       }
      return _tooBarView;
     }
    
     -(UIButton *)tooBarViewBtn{
    
        if (!_tooBarViewBtn) {
      
           _tooBarViewBtn = [[UIButton alloc]initWithFrame:CGRectMake(CIO_SCREEN_WIDTH-150, 0, 100, 44)];
           [_tooBarViewBtn addTarget:self action:@selector(switchKeyBoard) forControlEvents:UIControlEventTouchUpInside];
           [_tooBarViewBtn setTitle:@"    " forState:UIControlStateNormal];
           _tooBarViewBtn.backgroundColor = [UIColor purpleColor];
        }
       return _tooBarViewBtn;
     }
    
     -(UIView *)keyBoardView{
    
         if (!_keyBoardView) {
      
             _keyBoardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CIO_SCREEN_WIDTH, 100)];
            _keyBoardView.backgroundColor = [UIColor brownColor];
         }
    
       return _keyBoardView;
     }
    
     @end
    
  • を切り替えます.