Skip to content

Commit

Permalink
Fix issue #8 (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntnmrndn committed Oct 6, 2020
1 parent c01f4ba commit e275c45
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions NextGrowingTextView/NextGrowingTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ open class NextGrowingTextView: UIScrollView {

private func setup() {

NotificationCenter.default.addObserver(self, selector: #selector(willShowMenu(_:)), name: UIMenuController.willShowMenuNotification, object: nil)
_textView.textContainerInset = .init(top: 4, left: 0, bottom: 4, right: 0)
_textView.isScrollEnabled = false
_textView.font = UIFont.systemFont(ofSize: 16)
Expand All @@ -181,6 +182,21 @@ open class NextGrowingTextView: UIScrollView {
}
}

private var isFixingMenuPosition: Bool = false
@objc private func willShowMenu(_ notification: Notification) {
guard let menuController = notification.object as? UIMenuController,
let superview = superview,
isFixingMenuPosition == false,
_textView.isFirstResponder,
!menuController.menuFrame.intersects(superview.convert(frame, to: nil))
else { return }
menuController.setMenuVisible(false, animated: false)
menuController.setTargetRect(frame, in: superview)
isFixingMenuPosition = true
menuController.setMenuVisible(true, animated: true)
isFixingMenuPosition = false
}

private func measureTextViewSize() -> CGSize {
let size = _textView.sizeThatFits(CGSize(width: self.bounds.width, height: CGFloat.infinity))
return .init(width: size.width, height: max(size.height, _minHeight))
Expand Down

0 comments on commit e275c45

Please sign in to comment.