diff --git a/SignalMessaging/attachments/AttachmentApprovalViewController.swift b/SignalMessaging/attachments/AttachmentApprovalViewController.swift index 8f4f0f965..6df18cf4c 100644 --- a/SignalMessaging/attachments/AttachmentApprovalViewController.swift +++ b/SignalMessaging/attachments/AttachmentApprovalViewController.swift @@ -405,27 +405,7 @@ class CaptioningToolbar: UIView, UITextViewDelegate { self.captioningToolbarDelegate?.captioningToolbarDidTapSend(self, captionText: self.textView.text) } - // MARK: - UITextViewDelegate - - public func textViewDidChange(_ textView: UITextView) { - Logger.debug("\(self.logTag) in \(#function)") - - // compute new height assuming width is unchanged - let currentSize = textView.frame.size - let newHeight = clampedTextViewHeight(fixedWidth: currentSize.width) - - if newHeight != self.textViewHeight { - Logger.debug("\(self.logTag) TextView height changed: \(self.textViewHeight) -> \(newHeight)") - self.textViewHeight = newHeight - self.setNeedsLayout() - self.layoutIfNeeded() - } - } - - private func clampedTextViewHeight(fixedWidth: CGFloat) -> CGFloat { - let contentSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude)) - return Clamp(contentSize.height, kMinTextViewHeight, maxTextViewHeight) - } + // MARK: - UIView Overrides // We do progammatic layout, explicitly computing and setting frames since autoLayout does // not seem to work with inputAccessory views, even when forcing a layout. @@ -463,4 +443,40 @@ class CaptioningToolbar: UIView, UITextViewDelegate { Logger.debug("After layout >>> self: \(self.frame) textView: \(self.textView.frame), sendButton:\(sendButton.frame)") } + + // MARK: - UITextViewDelegate + + public func textViewDidChange(_ textView: UITextView) { + Logger.debug("\(self.logTag) in \(#function)") + + // compute new height assuming width is unchanged + let currentSize = textView.frame.size + let newHeight = clampedTextViewHeight(fixedWidth: currentSize.width) + + if newHeight != self.textViewHeight { + Logger.debug("\(self.logTag) TextView height changed: \(self.textViewHeight) -> \(newHeight)") + self.textViewHeight = newHeight + self.setNeedsLayout() + self.layoutIfNeeded() + } + } + + public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { + // Though we can wrap the text, we don't want to encourage multline captions, plus a "done" button + // allows the user to get the keyboard out of the way while in the attachment approval view. + if text == "\n" { + textView.resignFirstResponder() + return false + } else { + return true + } + } + + // MARK: - Helpers + + private func clampedTextViewHeight(fixedWidth: CGFloat) -> CGFloat { + let contentSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude)) + return Clamp(contentSize.height, kMinTextViewHeight, maxTextViewHeight) + } + }