Commit bf70e91f by Daniel Dahan

development: added handlers for keyboard changes in TextView

parent 52161cec
...@@ -399,6 +399,8 @@ open class TextField: UITextField { ...@@ -399,6 +399,8 @@ open class TextField: UITextField {
borderStyle = .none borderStyle = .none
backgroundColor = nil backgroundColor = nil
contentScaleFactor = Screen.scale contentScaleFactor = Screen.scale
font = RobotoFont.regular(with: 16)
textColor = Color.darkText.primary
prepareDivider() prepareDivider()
preparePlaceholderLabel() preparePlaceholderLabel()
...@@ -426,8 +428,8 @@ extension TextField { ...@@ -426,8 +428,8 @@ extension TextField {
/// Prepares the placeholderLabel. /// Prepares the placeholderLabel.
fileprivate func preparePlaceholderLabel() { fileprivate func preparePlaceholderLabel() {
font = RobotoFont.regular(with: 16)
placeholderNormalColor = Color.darkText.others placeholderNormalColor = Color.darkText.others
placeholderLabel.backgroundColor = .clear
addSubview(placeholderLabel) addSubview(placeholderLabel)
} }
......
...@@ -49,6 +49,22 @@ public protocol TextViewDelegate : UITextViewDelegate { ...@@ -49,6 +49,22 @@ public protocol TextViewDelegate : UITextViewDelegate {
optional func textView(textView: TextView, willHideKeyboard value: NSValue) optional func textView(textView: TextView, willHideKeyboard value: NSValue)
/** /**
A delegation method that is executed when the keyboard did open.
- Parameter textView: A TextView.
- Parameter didShowKeyboard value: A NSValue.
*/
@objc
optional func textView(textView: TextView, didShowKeyboard value: NSValue)
/**
A delegation method that is executed when the keyboard did close.
- Parameter textView: A TextView.
- Parameter didHideKeyboard value: A NSValue.
*/
@objc
optional func textView(textView: TextView, didHideKeyboard value: NSValue)
/**
A delegation method that is executed when text will be A delegation method that is executed when text will be
processed during editing. processed during editing.
- Parameter textView: A TextView. - Parameter textView: A TextView.
...@@ -265,6 +281,8 @@ open class TextView: UITextView { ...@@ -265,6 +281,8 @@ open class TextView: UITextView {
contentScaleFactor = Screen.scale contentScaleFactor = Screen.scale
textContainerInset = .zero textContainerInset = .zero
backgroundColor = nil backgroundColor = nil
font = RobotoFont.regular(with: 16)
textColor = Color.darkText.primary
prepareNotificationHandlers() prepareNotificationHandlers()
prepareRegularExpression() prepareRegularExpression()
preparePlaceholderLabel() preparePlaceholderLabel()
...@@ -277,6 +295,8 @@ extension TextView { ...@@ -277,6 +295,8 @@ extension TextView {
let defaultCenter = NotificationCenter.default let defaultCenter = NotificationCenter.default
defaultCenter.addObserver(self, selector: #selector(handleKeyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) defaultCenter.addObserver(self, selector: #selector(handleKeyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
defaultCenter.addObserver(self, selector: #selector(handleKeyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) defaultCenter.addObserver(self, selector: #selector(handleKeyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
defaultCenter.addObserver(self, selector: #selector(handleKeyboardDidShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
defaultCenter.addObserver(self, selector: #selector(handleKeyboardDidHide(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
defaultCenter.addObserver(self, selector: #selector(handleTextViewTextDidBegin), name: NSNotification.Name.UITextViewTextDidBeginEditing, object: self) defaultCenter.addObserver(self, selector: #selector(handleTextViewTextDidBegin), name: NSNotification.Name.UITextViewTextDidBeginEditing, object: self)
defaultCenter.addObserver(self, selector: #selector(handleTextViewTextDidChange), name: NSNotification.Name.UITextViewTextDidChange, object: self) defaultCenter.addObserver(self, selector: #selector(handleTextViewTextDidChange), name: NSNotification.Name.UITextViewTextDidChange, object: self)
defaultCenter.addObserver(self, selector: #selector(handleTextViewTextDidEnd), name: NSNotification.Name.UITextViewTextDidEndEditing, object: self) defaultCenter.addObserver(self, selector: #selector(handleTextViewTextDidEnd), name: NSNotification.Name.UITextViewTextDidEndEditing, object: self)
...@@ -289,7 +309,7 @@ extension TextView { ...@@ -289,7 +309,7 @@ extension TextView {
/// prepares the placeholderLabel property. /// prepares the placeholderLabel property.
fileprivate func preparePlaceholderLabel() { fileprivate func preparePlaceholderLabel() {
placeholderLabel.font = font placeholderLabel.textColor = Color.darkText.others
placeholderLabel.textAlignment = textAlignment placeholderLabel.textAlignment = textAlignment
placeholderLabel.numberOfLines = 0 placeholderLabel.numberOfLines = 0
placeholderLabel.backgroundColor = .clear placeholderLabel.backgroundColor = .clear
...@@ -336,13 +356,30 @@ extension TextView { ...@@ -336,13 +356,30 @@ extension TextView {
return return
} }
guard let v = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue else {
return
}
(delegate as? TextViewDelegate)?.textView?(textView: self, willShowKeyboard: v)
}
/**
Handler for when the keyboard did open.
- Parameter notification: A Notification.
*/
@objc
fileprivate func handleKeyboardDidShow(notification: Notification) {
guard isKeyboardHidden else {
return
}
isKeyboardHidden = false isKeyboardHidden = false
guard let v = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { guard let v = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue else {
return return
} }
(delegate as? TextViewDelegate)?.textView?(textView: self, willShowKeyboard: v) (delegate as? TextViewDelegate)?.textView?(textView: self, didShowKeyboard: v)
} }
/** /**
...@@ -355,13 +392,30 @@ extension TextView { ...@@ -355,13 +392,30 @@ extension TextView {
return return
} }
guard let v = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue else {
return
}
(delegate as? TextViewDelegate)?.textView?(textView: self, willHideKeyboard: v)
}
/**
Handler for when the keyboard did close.
- Parameter notification: A Notification.
*/
@objc
fileprivate func handleKeyboardDidHide(notification: Notification) {
guard !isKeyboardHidden else {
return
}
isKeyboardHidden = true isKeyboardHidden = true
guard let v = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue else { guard let v = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue else {
return return
} }
(delegate as? TextViewDelegate)?.textView?(textView: self, willHideKeyboard: v) (delegate as? TextViewDelegate)?.textView?(textView: self, didHideKeyboard: v)
} }
/// Notification handler for when text editing began. /// Notification handler for when text editing began.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment