From 636e66b048e6427a2c3bbc7c419fb49f106e1e3e Mon Sep 17 00:00:00 2001 From: Ali <> Date: Sat, 2 Nov 2019 22:55:46 +0400 Subject: [PATCH] Fix text format menu on iOS 13.2 --- submodules/AsyncDisplayKit/Source/ASEditableTextNode.h | 2 ++ .../AsyncDisplayKit/Source/ASEditableTextNode.mm | 9 +++++++++ .../TelegramUI/TelegramUI/ChatTextInputMenu.swift | 10 +++++++--- .../TelegramUI/TelegramUI/ChatTextInputPanelNode.swift | 10 +++++++++- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/submodules/AsyncDisplayKit/Source/ASEditableTextNode.h b/submodules/AsyncDisplayKit/Source/ASEditableTextNode.h index 397d815edb..b30969e3f1 100644 --- a/submodules/AsyncDisplayKit/Source/ASEditableTextNode.h +++ b/submodules/AsyncDisplayKit/Source/ASEditableTextNode.h @@ -71,6 +71,8 @@ NS_ASSUME_NONNULL_BEGIN //! @abstract The range of text currently selected. If length is zero, the range is the cursor location. @property NSRange selectedRange; +@property (readonly) CGRect selectionRect; + #pragma mark - Placeholder /** @abstract Indicates if the receiver is displaying the placeholder text. diff --git a/submodules/AsyncDisplayKit/Source/ASEditableTextNode.mm b/submodules/AsyncDisplayKit/Source/ASEditableTextNode.mm index 277af0f9c8..21f94b4493 100644 --- a/submodules/AsyncDisplayKit/Source/ASEditableTextNode.mm +++ b/submodules/AsyncDisplayKit/Source/ASEditableTextNode.mm @@ -566,6 +566,15 @@ _textKitComponents.textView.selectedRange = selectedRange; } +- (CGRect)selectionRect { + UITextRange *range = [_textKitComponents.textView selectedTextRange]; + if (range != nil) { + return [_textKitComponents.textView firstRectForRange:range]; + } else { + return [_textKitComponents.textView bounds]; + } +} + #pragma mark - Placeholder - (BOOL)isDisplayingPlaceholder { diff --git a/submodules/TelegramUI/TelegramUI/ChatTextInputMenu.swift b/submodules/TelegramUI/TelegramUI/ChatTextInputMenu.swift index 3dfb32530c..0db7a756ca 100644 --- a/submodules/TelegramUI/TelegramUI/ChatTextInputMenu.swift +++ b/submodules/TelegramUI/TelegramUI/ChatTextInputMenu.swift @@ -33,8 +33,6 @@ final class ChatTextInputMenu { UIMenuItem(title: self.stringStrikethrough, action: Selector(("formatAttributesStrikethrough:"))), UIMenuItem(title: self.stringUnderline, action: Selector(("formatAttributesUnderline:"))) ] - UIMenuController.shared.isMenuVisible = true - UIMenuController.shared.update() } } @@ -74,9 +72,15 @@ final class ChatTextInputMenu { self.state = .inactive } - func format() { + func format(view: UIView, rect: CGRect) { if self.state == .general { self.state = .format + if #available(iOS 13.0, *) { + UIMenuController.shared.showMenu(from: view, rect: rect) + } else { + UIMenuController.shared.isMenuVisible = true + UIMenuController.shared.update() + } } } diff --git a/submodules/TelegramUI/TelegramUI/ChatTextInputPanelNode.swift b/submodules/TelegramUI/TelegramUI/ChatTextInputPanelNode.swift index a310fdbf8f..345b205cd2 100644 --- a/submodules/TelegramUI/TelegramUI/ChatTextInputPanelNode.swift +++ b/submodules/TelegramUI/TelegramUI/ChatTextInputPanelNode.swift @@ -1437,6 +1437,12 @@ class ChatTextInputPanelNode: ChatInputPanelNode, ASEditableTextNodeDelegate { } if let textInputNode = self.textInputNode, let presentationInterfaceState = self.presentationInterfaceState { + + if case .format = self.inputMenu.state { + self.inputMenu.deactivate() + UIMenuController.shared.update() + } + let baseFontSize = max(17.0, presentationInterfaceState.fontSize.baseDisplaySize) refreshChatTextInputTypingAttributes(textInputNode, theme: presentationInterfaceState.theme, baseFontSize: baseFontSize) } @@ -1478,7 +1484,9 @@ class ChatTextInputPanelNode: ChatInputPanelNode, ASEditableTextNodeDelegate { } @objc func _showTextStyleOptions(_ sender: Any) { - self.inputMenu.format() + if let textInputNode = self.textInputNode { + self.inputMenu.format(view: textInputNode.view, rect: textInputNode.selectionRect.insetBy(dx: 0.0, dy: -1.0)) + } } @objc func formatAttributesBold(_ sender: Any) {