diff --git a/STTextView/STTextView.podspec b/STTextView/STTextView.podspec index 60fd649..bf59a0e 100644 --- a/STTextView/STTextView.podspec +++ b/STTextView/STTextView.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "STTextView" - spec.version = "1.1.0" + spec.version = "1.1.1" spec.summary = "STTextView is a light-weight CocoaPod that adds a placeholder to the UITextView." diff --git a/STTextView/STTextView.xcodeproj/project.xcworkspace/xcuserdata/onl1ner.xcuserdatad/UserInterfaceState.xcuserstate b/STTextView/STTextView.xcodeproj/project.xcworkspace/xcuserdata/onl1ner.xcuserdatad/UserInterfaceState.xcuserstate index bd61928..73cee68 100644 Binary files a/STTextView/STTextView.xcodeproj/project.xcworkspace/xcuserdata/onl1ner.xcuserdatad/UserInterfaceState.xcuserstate and b/STTextView/STTextView.xcodeproj/project.xcworkspace/xcuserdata/onl1ner.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/STTextView/STTextView/STTextView.swift b/STTextView/STTextView/STTextView.swift index 0960d0b..1fd0f31 100644 --- a/STTextView/STTextView/STTextView.swift +++ b/STTextView/STTextView/STTextView.swift @@ -22,7 +22,7 @@ import UIKit -@IBDesignable open class STTextView : UITextView, UITextViewDelegate { +@IBDesignable open class STTextView : UITextView { /// Placeholder string that will be shown in your TextView. @IBInspectable public var placeholder : String = "Enter your placeholder" { @@ -85,27 +85,24 @@ import UIKit return textView }() - public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool { - if textView.text.isEmpty { + @objc private func textDidBeginEditing() -> () { + if self.text.isEmpty { if shouldHidePlaceholderOnEditing { placeholderTextView.isHidden = true } } - return true } - public func textViewShouldEndEditing(_ textView: UITextView) -> Bool { - placeholderTextView.isHidden = !textView.text.isEmpty - return true + @objc private func textDidChange() -> () { + placeholderTextView.isHidden = !self.text.isEmpty } - - public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { - let currentContent : NSString = NSString(string: textView.text) - let newContent = currentContent.replacingCharacters(in: range, with: text) - - self.placeholderTextView.isHidden = !newContent.isEmpty - - return true + + @objc private func textDidEndEditing() -> () { + if self.text.isEmpty { + if shouldHidePlaceholderOnEditing { + placeholderTextView.isHidden = false + } + } } // Method is used to update the placeholderTextView whenever @@ -118,10 +115,16 @@ import UIKit placeholderTextView.textAlignment = self.textAlignment } + private func signForNotifications() -> () { + NotificationCenter.default.addObserver(self, selector: #selector(textDidChange), name: UITextView.textDidChangeNotification, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(textDidBeginEditing), name: UITextView.textDidBeginEditingNotification, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(textDidEndEditing), name: UITextView.textDidEndEditingNotification, object: nil) + } + private func initialConfiguration() -> () { - self.delegate = self - self.insertSubview(placeholderTextView, at: 0) + + signForNotifications() } public override func prepareForInterfaceBuilder() {