From 7041eb4107eb6ccf6dc3682374c0d28f4218758b Mon Sep 17 00:00:00 2001 From: Igor Kislyuk Date: Sun, 12 Nov 2017 01:25:06 +0300 Subject: [PATCH] Update - fix swift 4 warnings - add posibility to disable actions - add functionality to disable moving cursor --- .../Source/EditableTextField.swift | 61 ++++++++++++++++++- .../Source/UIAnimatedTextField.swift | 6 +- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/UIAnimatedTextField/Source/EditableTextField.swift b/UIAnimatedTextField/Source/EditableTextField.swift index c57c0ce..41fb7ef 100644 --- a/UIAnimatedTextField/Source/EditableTextField.swift +++ b/UIAnimatedTextField/Source/EditableTextField.swift @@ -24,10 +24,33 @@ import Foundation +enum EditableActionType { + case selectAll + case select + case cut + case copy + case paste + + static let allActions = [EditableActionType.selectAll, .select, .cut, .paste, .copy] +} + public class EditableTextField: UITextField { - + + /// Actions, that will be disabled for this textField. + /// By default no actions are disabled. + var disabledActions: [EditableActionType] = [] + + /// Allows to disable moving cursor for user + var pinCursorToEnd: Bool = false + var getType: (() -> TextType?)? - + + // MARK: - Private + + private var disabledSelectors: [Selector] { + return disabledActions.map { selector(from: $0) } + } + private let menuSelectors = [ #selector(selectAll(_:)), #selector(select(_:)), @@ -35,8 +58,14 @@ public class EditableTextField: UITextField { #selector(copy(_:)), #selector(paste(_:)) ] + + // MARK: - Overriden override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { + if disabledSelectors.contains(action) { + return false + } + guard let type = getType?() else { return super.canPerformAction(action, withSender: sender) } @@ -61,5 +90,33 @@ public class EditableTextField: UITextField { return super.caretRect(for: position) } + + override public func closestPosition(to point: CGPoint) -> UITextPosition? { + if pinCursorToEnd { + return endOfDocument + } + + return super.closestPosition(to: point) + } } + +// MARK: - Private extensions + +private extension UITextField { + + func selector(from actionTyoe: EditableActionType) -> Selector { + switch actionTyoe { + case .selectAll: + return #selector(selectAll(_:)) + case .select: + return #selector(select(_:)) + case .cut: + return #selector(cut(_:)) + case .copy: + return #selector(copy(_:)) + case .paste: + return #selector(paste(_:)) + } + } +} diff --git a/UIAnimatedTextField/Source/UIAnimatedTextField.swift b/UIAnimatedTextField/Source/UIAnimatedTextField.swift index 1d4562b..be0b274 100644 --- a/UIAnimatedTextField/Source/UIAnimatedTextField.swift +++ b/UIAnimatedTextField/Source/UIAnimatedTextField.swift @@ -223,7 +223,7 @@ open class UIAnimatedTextField: UIView { private var state: AnimatedTextFieldState { var state: AnimatedTextFieldState = .placeholder - if textField.text?.characters.count ?? 0 > 0 || textField.isFirstResponder { + if textField.text?.count ?? 0 > 0 || textField.isFirstResponder { state = .text } @@ -491,7 +491,7 @@ extension UIAnimatedTextField: UITextFieldDelegate { } open func textFieldDidBeginEditing(_ textField: UITextField) { - if textField.text?.characters.count ?? 0 == 0 { + if textField.text?.count ?? 0 == 0 { setState(toState: .text, duration: UIAnimatedTextField.animationDuration) } @@ -515,7 +515,7 @@ extension UIAnimatedTextField: UITextFieldDelegate { } open func textFieldDidEndEditing(_ textField: UITextField) { - if textField.text?.characters.count ?? 0 == 0 { + if textField.text?.count ?? 0 == 0 { setState(toState: .placeholder, duration: UIAnimatedTextField.animationDuration) }