- fix swift 4 warnings
- add posibility to disable actions
- add functionality to disable moving cursor
This commit is contained in:
Igor Kislyuk 2017-11-12 01:25:06 +03:00
parent 7a0d673396
commit 7041eb4107
2 changed files with 62 additions and 5 deletions

View File

@ -24,10 +24,33 @@
import Foundation 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 { 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?)? var getType: (() -> TextType?)?
// MARK: - Private
private var disabledSelectors: [Selector] {
return disabledActions.map { selector(from: $0) }
}
private let menuSelectors = [ private let menuSelectors = [
#selector(selectAll(_:)), #selector(selectAll(_:)),
#selector(select(_:)), #selector(select(_:)),
@ -36,7 +59,13 @@ public class EditableTextField: UITextField {
#selector(paste(_:)) #selector(paste(_:))
] ]
// MARK: - Overriden
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if disabledSelectors.contains(action) {
return false
}
guard let type = getType?() else { guard let type = getType?() else {
return super.canPerformAction(action, withSender: sender) return super.canPerformAction(action, withSender: sender)
} }
@ -62,4 +91,32 @@ public class EditableTextField: UITextField {
return super.caretRect(for: position) 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(_:))
}
}
} }

View File

@ -223,7 +223,7 @@ open class UIAnimatedTextField: UIView {
private var state: AnimatedTextFieldState { private var state: AnimatedTextFieldState {
var state: AnimatedTextFieldState = .placeholder var state: AnimatedTextFieldState = .placeholder
if textField.text?.characters.count ?? 0 > 0 || textField.isFirstResponder { if textField.text?.count ?? 0 > 0 || textField.isFirstResponder {
state = .text state = .text
} }
@ -491,7 +491,7 @@ extension UIAnimatedTextField: UITextFieldDelegate {
} }
open func textFieldDidBeginEditing(_ textField: UITextField) { open func textFieldDidBeginEditing(_ textField: UITextField) {
if textField.text?.characters.count ?? 0 == 0 { if textField.text?.count ?? 0 == 0 {
setState(toState: .text, duration: UIAnimatedTextField.animationDuration) setState(toState: .text, duration: UIAnimatedTextField.animationDuration)
} }
@ -515,7 +515,7 @@ extension UIAnimatedTextField: UITextFieldDelegate {
} }
open func textFieldDidEndEditing(_ textField: UITextField) { open func textFieldDidEndEditing(_ textField: UITextField) {
if textField.text?.characters.count ?? 0 == 0 { if textField.text?.count ?? 0 == 0 {
setState(toState: .placeholder, duration: UIAnimatedTextField.animationDuration) setState(toState: .placeholder, duration: UIAnimatedTextField.animationDuration)
} }