Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Public access to textfield and textview
Browse files Browse the repository at this point in the history
  • Loading branch information
victorBaro committed Apr 17, 2017
1 parent d4ad9a7 commit 827475a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
45 changes: 23 additions & 22 deletions AnimatedTextInput/Classes/AnimatedTextField.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import UIKit

final internal class AnimatedTextField: UITextField {
final public class AnimatedTextField: UITextField {

enum TextFieldType {
case text
Expand All @@ -13,9 +13,9 @@ final internal class AnimatedTextField: UITextField {
fileprivate let clearButtonPadding: CGFloat = -8

var rightViewPadding: CGFloat
weak var textInputDelegate: TextInputDelegate?
weak public var textInputDelegate: TextInputDelegate?

var textAttributes: [String: Any]?
public var textAttributes: [String: Any]?

fileprivate var disclosureButtonAction: ((Void) -> Void)?

Expand All @@ -27,9 +27,10 @@ final internal class AnimatedTextField: UITextField {
setup()
}

required init?(coder aDecoder: NSCoder) {
required public init?(coder aDecoder: NSCoder) {
self.rightViewPadding = defaultPadding


super.init(coder: aDecoder)

setup()
Expand All @@ -40,30 +41,30 @@ final internal class AnimatedTextField: UITextField {
addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
}

@discardableResult override func becomeFirstResponder() -> Bool {
@discardableResult override public func becomeFirstResponder() -> Bool {
if let alignment = (textAttributes?[NSParagraphStyleAttributeName] as? NSMutableParagraphStyle)?.alignment {
textAlignment = alignment
}
return super.becomeFirstResponder()
}

override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
override public func rightViewRect(forBounds bounds: CGRect) -> CGRect {
return super.rightViewRect(forBounds: bounds).offsetBy(dx: rightViewPadding, dy: 0)
}

override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
override public func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
return super.clearButtonRect(forBounds: bounds).offsetBy(dx: clearButtonPadding, dy: 0)
}

override func textRect(forBounds bounds: CGRect) -> CGRect {
override public func textRect(forBounds bounds: CGRect) -> CGRect {
var width = bounds.width
if clearButtonMode == .always || clearButtonMode == .unlessEditing {
width = bounds.width - clearButtonRect(forBounds: bounds).width * 2
}
return CGRect(x: bounds.origin.x, y: bounds.origin.y, width: width, height: bounds.height)
}

override func editingRect(forBounds bounds: CGRect) -> CGRect {
override public func editingRect(forBounds bounds: CGRect) -> CGRect {
var width = bounds.width
if clearButtonMode != .never {
width = bounds.width - clearButtonRect(forBounds: bounds).width * 2
Expand Down Expand Up @@ -97,24 +98,24 @@ final internal class AnimatedTextField: UITextField {

extension AnimatedTextField: TextInput {

func changeReturnKeyType(with newReturnKeyType: UIReturnKeyType) {
public func changeReturnKeyType(with newReturnKeyType: UIReturnKeyType) {
returnKeyType = newReturnKeyType
}

func currentPosition(from: UITextPosition, offset: Int) -> UITextPosition? {
public func currentPosition(from: UITextPosition, offset: Int) -> UITextPosition? {
return position(from: from, offset: offset)
}

func changeClearButtonMode(with newClearButtonMode: UITextFieldViewMode) {
public func changeClearButtonMode(with newClearButtonMode: UITextFieldViewMode) {
clearButtonMode = newClearButtonMode
}

var currentText: String? {
public var currentText: String? {
get { return text }
set { self.text = newValue }
}

var currentSelectedTextRange: UITextRange? {
public var currentSelectedTextRange: UITextRange? {
get { return self.selectedTextRange }
set { self.selectedTextRange = newValue }
}
Expand All @@ -126,38 +127,38 @@ extension AnimatedTextField: TextInput {

extension AnimatedTextField: TextInputError {

func configureErrorState(with message: String?) {
public func configureErrorState(with message: String?) {
placeholder = message
}

func removeErrorHintMessage() {
public func removeErrorHintMessage() {
placeholder = nil
}
}

extension AnimatedTextField: UITextFieldDelegate {

func textFieldDidBeginEditing(_ textField: UITextField) {
public func textFieldDidBeginEditing(_ textField: UITextField) {
textInputDelegate?.textInputDidBeginEditing(textInput: self)
}

func textFieldDidEndEditing(_ textField: UITextField) {
public func textFieldDidEndEditing(_ textField: UITextField) {
textInputDelegate?.textInputDidEndEditing(textInput: self)
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return textInputDelegate?.textInput(textInput: self, shouldChangeCharactersInRange: range, replacementString: string) ?? true
}

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return textInputDelegate?.textInputShouldBeginEditing(textInput: self) ?? true
}

func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
public func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return textInputDelegate?.textInputShouldEndEditing(textInput: self) ?? true
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
return textInputDelegate?.textInputShouldReturn(textInput: self) ?? true
}
}
34 changes: 17 additions & 17 deletions AnimatedTextInput/Classes/AnimatedTextView.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import UIKit

final internal class AnimatedTextView: UITextView {
final public class AnimatedTextView: UITextView {

var textAttributes: [String: Any]? {
public var textAttributes: [String: Any]? {
didSet {
guard let attributes = textAttributes else { return }
typingAttributes = attributes
}
}

weak var textInputDelegate: TextInputDelegate?
public weak var textInputDelegate: TextInputDelegate?

override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)

setup()
}

required init?(coder aDecoder: NSCoder) {
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

setup()
Expand All @@ -27,69 +27,69 @@ final internal class AnimatedTextView: UITextView {
delegate = self
}

override func resignFirstResponder() -> Bool {
public override func resignFirstResponder() -> Bool {
return super.resignFirstResponder()
}
}

extension AnimatedTextView: TextInput {

var currentText: String? {
public var currentText: String? {
get { return text }
set { self.text = newValue }
}

var currentSelectedTextRange: UITextRange? {
public var currentSelectedTextRange: UITextRange? {
get { return self.selectedTextRange }
set { self.selectedTextRange = newValue }
}

open var currentBeginningOfDocument: UITextPosition? {
public var currentBeginningOfDocument: UITextPosition? {
return self.beginningOfDocument
}

func changeReturnKeyType(with newReturnKeyType: UIReturnKeyType) {
public func changeReturnKeyType(with newReturnKeyType: UIReturnKeyType) {
returnKeyType = newReturnKeyType
}

func currentPosition(from: UITextPosition, offset: Int) -> UITextPosition? {
public func currentPosition(from: UITextPosition, offset: Int) -> UITextPosition? {
return position(from: from, offset: offset)
}

func changeClearButtonMode(with newClearButtonMode: UITextFieldViewMode) {}
public func changeClearButtonMode(with newClearButtonMode: UITextFieldViewMode) {}

}

extension AnimatedTextView: UITextViewDelegate {

func textViewDidBeginEditing(_ textView: UITextView) {
public func textViewDidBeginEditing(_ textView: UITextView) {
textInputDelegate?.textInputDidBeginEditing(textInput: self)
}

func textViewDidEndEditing(_ textView: UITextView) {
public func textViewDidEndEditing(_ textView: UITextView) {
textInputDelegate?.textInputDidEndEditing(textInput: self)
}

func textViewDidChange(_ textView: UITextView) {
public func textViewDidChange(_ textView: UITextView) {
let range = textView.selectedRange
textView.attributedText = NSAttributedString(string: textView.text, attributes: textAttributes)
textView.selectedRange = range

textInputDelegate?.textInputDidChange(textInput: self)
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
return textInputDelegate?.textInputShouldReturn(textInput: self) ?? true
}
return textInputDelegate?.textInput(textInput: self, shouldChangeCharactersInRange: range, replacementString: text) ?? true
}

func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
return textInputDelegate?.textInputShouldBeginEditing(textInput: self) ?? true
}

func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
public func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
return textInputDelegate?.textInputShouldEndEditing(textInput: self) ?? true
}
}

0 comments on commit 827475a

Please sign in to comment.