Update
- fix swift 4 warnings - add posibility to disable actions - add functionality to disable moving cursor
This commit is contained in:
parent
7a0d673396
commit
7041eb4107
|
|
@ -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(_:)),
|
||||
|
|
@ -36,7 +59,13 @@ public class EditableTextField: UITextField {
|
|||
#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)
|
||||
}
|
||||
|
|
@ -62,4 +91,32 @@ 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(_:))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue