Skip to content

Commit

Permalink
Fixed delegation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
onl1ner committed Aug 9, 2020
1 parent 1311601 commit e450757
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion STTextView/STTextView.podspec
Original file line number Diff line number Diff line change
@@ -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."

Expand Down
Binary file not shown.
37 changes: 20 additions & 17 deletions STTextView/STTextView/STTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand Down

0 comments on commit e450757

Please sign in to comment.