From 86d60c1a21033d4fa575fd6fbe83482d5061c4a7 Mon Sep 17 00:00:00 2001 From: Ruslan Skorb Date: Thu, 18 Jul 2019 21:17:55 +0300 Subject: [PATCH] [Sources] [RSKGrowingTextView] Handle `UITextView.textDidChangeNotification`. Fixes #18. --- RSKGrowingTextView/RSKGrowingTextView.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/RSKGrowingTextView/RSKGrowingTextView.swift b/RSKGrowingTextView/RSKGrowingTextView.swift index 096052e..2909dcf 100644 --- a/RSKGrowingTextView/RSKGrowingTextView.swift +++ b/RSKGrowingTextView/RSKGrowingTextView.swift @@ -180,6 +180,11 @@ public typealias HeightChangeUserActionsBlockType = ((_ oldHeight: CGFloat, _ ne // MARK: - Object Lifecycle + deinit { + + NotificationCenter.default.removeObserver(self, name: UITextView.textDidChangeNotification, object: self) + } + required public init?(coder aDecoder: NSCoder) { calculationLayoutManager.addTextContainer(calculationTextContainer) super.init(coder: aDecoder) @@ -192,6 +197,13 @@ public typealias HeightChangeUserActionsBlockType = ((_ oldHeight: CGFloat, _ ne commonInitializer() } + // MARK: - Actions + + @objc private func handleRSKGrowingTextViewTextDidChangeNotification(_ notification: Notification) { + + refreshHeightIfNeededAnimated(animateHeightChange) + } + // MARK: - Private API private func commonInitializer() { @@ -206,6 +218,8 @@ public typealias HeightChangeUserActionsBlockType = ((_ oldHeight: CGFloat, _ ne break } } + + NotificationCenter.default.addObserver(self, selector: #selector(RSKGrowingTextView.handleRSKGrowingTextViewTextDidChangeNotification(_:)), name: UITextView.textDidChangeNotification, object: self) } private func heightForNumberOfLines(_ numberOfLines: Int) -> CGFloat {