|
@@ -0,0 +1,261 @@
|
|
|
+//
|
|
|
+// KeyBoardInputView.swift
|
|
|
+// RainbowPlanet
|
|
|
+//
|
|
|
+// Created by 南鑫林 on 2019/7/24.
|
|
|
+// Copyright © 2019 RainbowPlanet. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+import Foundation
|
|
|
+import IQKeyboardManagerSwift
|
|
|
+
|
|
|
+typealias InputViewResultClosure = (_ contentStr: String) -> Void
|
|
|
+
|
|
|
+typealias DismissClosure = () -> Void
|
|
|
+
|
|
|
+typealias InputAccessoryConfigeClosure = (_ inputConfiger: InputConfiger) -> Void
|
|
|
+
|
|
|
+class KeyBoardInputView: NSObject {
|
|
|
+
|
|
|
+ deinit {
|
|
|
+ NXLLog("deinit")
|
|
|
+ }
|
|
|
+
|
|
|
+ private var inputViewResultClosure : InputViewResultClosure!
|
|
|
+ private var dismissClosure : DismissClosure!
|
|
|
+
|
|
|
+ private var inputView : IQTextView!
|
|
|
+ private var btnSave : UIButton!
|
|
|
+ private var toolbar : UIView!
|
|
|
+
|
|
|
+ private var assistDismissButton : UIButton!
|
|
|
+ private var assistInputView : UITextField!
|
|
|
+
|
|
|
+ static let shared : KeyBoardInputView = KeyBoardInputView()
|
|
|
+
|
|
|
+ class func dimiss() {
|
|
|
+ KeyBoardInputView.shared.dismiss()
|
|
|
+ }
|
|
|
+
|
|
|
+ class func show(inputViewResultClosure:@escaping InputViewResultClosure,dismissClosure:@escaping DismissClosure) {
|
|
|
+ KeyBoardInputView.shared.initInputView(inputConfiger: InputConfiger(), inputViewResultClosure: inputViewResultClosure, dismissClosure: dismissClosure)
|
|
|
+ }
|
|
|
+
|
|
|
+ class func show(KeyboardType:UIKeyboardType,inputViewResultClosure:@escaping InputViewResultClosure,dismissClosure:@escaping DismissClosure) {
|
|
|
+ let inputConfiger = InputConfiger()
|
|
|
+ inputConfiger.keyboardType = KeyboardType
|
|
|
+ KeyBoardInputView.shared.initInputView(inputConfiger: inputConfiger, inputViewResultClosure: inputViewResultClosure, dismissClosure: dismissClosure)
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ class func show(KeyboardType:UIKeyboardType,content:String,inputViewResultClosure:@escaping InputViewResultClosure,dismissClosure:@escaping DismissClosure) {
|
|
|
+ let inputConfiger = InputConfiger()
|
|
|
+ inputConfiger.keyboardType = KeyboardType
|
|
|
+ inputConfiger.content = content
|
|
|
+ KeyBoardInputView.shared.initInputView(inputConfiger: inputConfiger, inputViewResultClosure: inputViewResultClosure, dismissClosure: dismissClosure)
|
|
|
+ }
|
|
|
+
|
|
|
+ class func show(inputAccessoryConfigeClosure:@escaping InputAccessoryConfigeClosure,inputViewResultClosure:@escaping InputViewResultClosure,dismissClosure:@escaping DismissClosure) {
|
|
|
+
|
|
|
+ let inputConfiger = InputConfiger()
|
|
|
+ inputAccessoryConfigeClosure(inputConfiger)
|
|
|
+ KeyBoardInputView.shared.initInputView(inputConfiger: inputConfiger, inputViewResultClosure: inputViewResultClosure, dismissClosure: dismissClosure)
|
|
|
+ }
|
|
|
+
|
|
|
+ private func initInputView(inputConfiger:InputConfiger,inputViewResultClosure:@escaping InputViewResultClosure,dismissClosure:@escaping DismissClosure) {
|
|
|
+ initView()
|
|
|
+ let window = kAppDelegate.window
|
|
|
+ self.assistDismissButton.frame = window!.bounds
|
|
|
+ self.assistDismissButton.autoresizingMask = UIView.AutoresizingMask(rawValue: UIView.AutoresizingMask.flexibleWidth.rawValue | UIView.AutoresizingMask.flexibleHeight.rawValue)
|
|
|
+ window?.addSubview(assistDismissButton)
|
|
|
+
|
|
|
+ self.updateLayout(beigin: true)
|
|
|
+
|
|
|
+ self.inputViewResultClosure = inputViewResultClosure
|
|
|
+ self.dismissClosure = dismissClosure
|
|
|
+ self.inputView.text = inputConfiger.content
|
|
|
+ self.assistInputView.text = inputConfiger.content
|
|
|
+
|
|
|
+ self.inputView.keyboardType = inputConfiger.keyboardType;
|
|
|
+ self.inputView.placeholder = inputConfiger.placeholder
|
|
|
+ self.assistInputView.keyboardType = inputConfiger.keyboardType;
|
|
|
+ self.btnSave.isEnabled = false
|
|
|
+ self.assistDismissButton.isUserInteractionEnabled = inputConfiger.backgroundUserInterface
|
|
|
+ self.assistDismissButton.backgroundColor = inputConfiger.backgroundColor
|
|
|
+
|
|
|
+ self.assistInputView.inputAccessoryView = toolbar
|
|
|
+ self.assistInputView.becomeFirstResponder()
|
|
|
+
|
|
|
+ NotificationCenter.default.addObserver(forName: UIResponder.keyboardDidShowNotification, object: nil, queue: nil) {[weak self] (notification) in
|
|
|
+ if ((self?.assistDismissButton?.window) != nil) && (self?.assistInputView.isFirstResponder)! {
|
|
|
+ self?.inputView.becomeFirstResponder()
|
|
|
+ self?.inputView.scrollRangeToVisible(NSRange(location: (self?.inputView.text.count)!, length: 1))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //输入切换
|
|
|
+ NotificationCenter.default.addObserver(forName: UITextInputMode.currentInputModeDidChangeNotification, object: nil, queue: nil) {
|
|
|
+ [weak self] (notification) in
|
|
|
+ self?.assistInputView.resignFirstResponder()
|
|
|
+ self?.assistInputView.becomeFirstResponder()
|
|
|
+ }
|
|
|
+
|
|
|
+ NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: nil) { [weak self] (notification) in
|
|
|
+ if (self?.inputView.isFirstResponder)! {
|
|
|
+ self?.dismiss()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ NotificationCenter.default.addObserver(forName: UIResponder.keyboardDidHideNotification, object: nil, queue: nil) { [weak self] (notification) in
|
|
|
+ self?.cleanInitView()
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private func initView() {
|
|
|
+
|
|
|
+ /// 背景button
|
|
|
+ let backgroundBtn = UIButton(type: UIButton.ButtonType.custom)
|
|
|
+ backgroundBtn.addTarget(self, action: #selector(dismiss), for: UIControl.Event.touchUpInside)
|
|
|
+
|
|
|
+ /// 工具bar
|
|
|
+ let toolbar = UIView()
|
|
|
+ toolbar.backgroundColor = UIColor.white
|
|
|
+
|
|
|
+
|
|
|
+ /// 输入框
|
|
|
+ let textView = IQTextView()
|
|
|
+ textView.returnKeyType = .done
|
|
|
+ textView.enablesReturnKeyAutomatically = true
|
|
|
+ textView.delegate = self
|
|
|
+ textView.font = kRegularFont14
|
|
|
+ textView.layer.borderColor = kf0f0f0Color.cgColor
|
|
|
+ textView.layer.borderWidth = 0.5/UIScreen.main.scale
|
|
|
+ textView.textContainerInset = UIEdgeInsets(top: 5.5, left: 15, bottom: 5.5, right: 15)
|
|
|
+// textView.textContainer.lineFragmentPadding = 0
|
|
|
+ textView.cornerRadius = 14
|
|
|
+ textView.masksToBounds = true
|
|
|
+ textView.tintColor = kThemeColor
|
|
|
+ textView.backgroundColor = kfafafaColor
|
|
|
+ toolbar.addSubview(textView)
|
|
|
+
|
|
|
+ let assistTxf = UITextField()
|
|
|
+ assistTxf.returnKeyType = .done
|
|
|
+ assistTxf.enablesReturnKeyAutomatically = true
|
|
|
+ backgroundBtn.addSubview(assistTxf)
|
|
|
+
|
|
|
+ /// 确定
|
|
|
+ let saveBtn = UIButton(type: UIButton.ButtonType.custom)
|
|
|
+ saveBtn.setBackgroundImage(UIImage.imageWithColor(color: kThemeColor), for: UIControl.State.normal)
|
|
|
+ saveBtn.setBackgroundImage(UIImage.imageWithColor(color: kd8d8d8Color), for: UIControl.State.disabled)
|
|
|
+ saveBtn.titleLabel?.font = kRegularFont14
|
|
|
+ saveBtn.setTitle("发布", for: UIControl.State.normal)
|
|
|
+ saveBtn.cornerRadius = 14
|
|
|
+ saveBtn.masksToBounds = true
|
|
|
+ saveBtn.setTitleColor(kffffffColor, for: UIControl.State.normal)
|
|
|
+ saveBtn.addTarget(self, action: #selector(save), for: UIControl.Event.touchUpInside)
|
|
|
+ toolbar.addSubview(saveBtn)
|
|
|
+
|
|
|
+ self.toolbar = toolbar
|
|
|
+ self.assistDismissButton = backgroundBtn
|
|
|
+ self.inputView = textView
|
|
|
+ self.assistInputView = assistTxf
|
|
|
+ self.btnSave = saveBtn
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ func updateLayout(beigin:Bool) {
|
|
|
+ let saveButtonWidth : CGFloat = 60
|
|
|
+ let edge : CGFloat = 10.0
|
|
|
+
|
|
|
+ var height = max(48, inputView.contentSize.height + 17.5)
|
|
|
+ height = min(height, 100)
|
|
|
+ UIView.animate(withDuration: beigin ? 0 : 0.2) {
|
|
|
+ [weak self] in
|
|
|
+ self?.toolbar.frame = CGRect(x: 0, y: 48-height, width: kScreenWidth, height: height)
|
|
|
+ self?.toolbar.configRectCorner(corner: [.topLeft,.topRight], radii: CGSize(width: 8, height: 8))
|
|
|
+ self?.inputView.frame = CGRect(x: 14, y: edge, width: kScreenWidth - saveButtonWidth - 38.0, height: height - edge * 2)
|
|
|
+ self?.btnSave.frame = CGRect(x: kScreenWidth - saveButtonWidth - 14, y: height - 10 - 28 , width: saveButtonWidth, height: 28.0)
|
|
|
+ self?.assistInputView.inputAccessoryView = self?.toolbar
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc func dismiss() {
|
|
|
+ self.inputView.resignFirstResponder()
|
|
|
+ self.assistDismissButton.removeFromSuperview()
|
|
|
+ NotificationCenter.default.removeObserver(self)
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc func save() {
|
|
|
+ self.inputView.resignFirstResponder()
|
|
|
+ if self.btnSave.isEnabled {
|
|
|
+ if let inputViewResultClosure = self.inputViewResultClosure {
|
|
|
+ inputViewResultClosure(self.inputView.text ?? "")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ self.dismiss()
|
|
|
+ }
|
|
|
+
|
|
|
+ func cleanInitView() {
|
|
|
+
|
|
|
+ if let dismissClosure = self.dismissClosure {
|
|
|
+ dismissClosure()
|
|
|
+ }
|
|
|
+ if self.toolbar != nil {
|
|
|
+ self.toolbar.removeFromSuperview()
|
|
|
+ }
|
|
|
+ if self.assistDismissButton != nil {
|
|
|
+ self.assistDismissButton.removeFromSuperview()
|
|
|
+ }
|
|
|
+ if self.inputView != nil {
|
|
|
+ self.inputView.removeFromSuperview()
|
|
|
+ }
|
|
|
+ if self.assistInputView != nil {
|
|
|
+ self.assistInputView.removeFromSuperview()
|
|
|
+ }
|
|
|
+ if self.btnSave != nil {
|
|
|
+ self.btnSave.removeFromSuperview()
|
|
|
+ }
|
|
|
+ self.toolbar = nil
|
|
|
+ self.assistDismissButton = nil
|
|
|
+ self.inputView = nil
|
|
|
+ self.assistInputView = nil
|
|
|
+ self.btnSave = nil
|
|
|
+ self.inputViewResultClosure = nil
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+extension KeyBoardInputView : UITextViewDelegate {
|
|
|
+ func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
|
|
|
+ if text == "\n" {
|
|
|
+ self.save()
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ func textViewDidChange(_ textView: UITextView) {
|
|
|
+ TextLimitTool.restrictionInputTextView(textView, maxNumber: 150)
|
|
|
+ self.updateLayout(beigin: false)
|
|
|
+ if textView.text.count > 0 {
|
|
|
+ self.btnSave.isEnabled = true
|
|
|
+ }else {
|
|
|
+ self.btnSave.isEnabled = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+class InputConfiger: NSObject {
|
|
|
+
|
|
|
+ // default UIKeyboardTypeDefault
|
|
|
+ var keyboardType : UIKeyboardType = UIKeyboardType.default
|
|
|
+ // default nil
|
|
|
+ var content : String = ""
|
|
|
+ // default YES
|
|
|
+ var backgroundUserInterface : Bool = true
|
|
|
+ // default clearColor
|
|
|
+ var backgroundColor : UIColor = UIColor(white: 0, alpha: 0.5)
|
|
|
+ // default placeholder
|
|
|
+ var placeholder : String = "添加评论..."
|
|
|
+}
|