From 99d0b4f43c545dea6cb70a97737ff41f42730bdf Mon Sep 17 00:00:00 2001 From: QichenZhu <57348009+QichenZhu@users.noreply.github.com> Date: Thu, 4 Jul 2024 14:11:34 +1200 Subject: [PATCH] Fix selection issue for text containing emoji --- ios/RCTBaseTextInputView+Markdown.mm | 9 ++++++++- ios/RCTTextInputComponentView+Markdown.mm | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/ios/RCTBaseTextInputView+Markdown.mm b/ios/RCTBaseTextInputView+Markdown.mm index 209dc6bb..7662d545 100644 --- a/ios/RCTBaseTextInputView+Markdown.mm +++ b/ios/RCTBaseTextInputView+Markdown.mm @@ -27,7 +27,14 @@ - (BOOL)markdown_textOf:(NSAttributedString *)newText equals:(NSAttributedString { RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils]; if (markdownUtils != nil) { - return [newText isEqualToAttributedString:oldText]; + // Emoji characters are automatically assigned an AppleColorEmoji NSFont and the original font is moved to NSOriginalFont + // We need to remove these attributes before comparison + NSMutableAttributedString *newTextCopy = [newText mutableCopy]; + NSMutableAttributedString *oldTextCopy = [oldText mutableCopy]; + [newTextCopy removeAttribute:@"NSFont" range:NSMakeRange(0, newTextCopy.length)]; + [oldTextCopy removeAttribute:@"NSFont" range:NSMakeRange(0, oldTextCopy.length)]; + [oldTextCopy removeAttribute:@"NSOriginalFont" range:NSMakeRange(0, oldTextCopy.length)]; + return [newTextCopy isEqualToAttributedString:oldTextCopy]; } return [self markdown_textOf:newText equals:oldText]; diff --git a/ios/RCTTextInputComponentView+Markdown.mm b/ios/RCTTextInputComponentView+Markdown.mm index 1a4581f3..3161e00d 100644 --- a/ios/RCTTextInputComponentView+Markdown.mm +++ b/ios/RCTTextInputComponentView+Markdown.mm @@ -51,7 +51,14 @@ - (BOOL)markdown__textOf:(NSAttributedString *)newText equals:(NSAttributedStrin { RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils]; if (markdownUtils != nil) { - return [newText isEqualToAttributedString:oldText]; + // Emoji characters are automatically assigned an AppleColorEmoji NSFont and the original font is moved to NSOriginalFont + // We need to remove these attributes before comparison + NSMutableAttributedString *newTextCopy = [newText mutableCopy]; + NSMutableAttributedString *oldTextCopy = [oldText mutableCopy]; + [newTextCopy removeAttribute:@"NSFont" range:NSMakeRange(0, newTextCopy.length)]; + [oldTextCopy removeAttribute:@"NSFont" range:NSMakeRange(0, oldTextCopy.length)]; + [oldTextCopy removeAttribute:@"NSOriginalFont" range:NSMakeRange(0, oldTextCopy.length)]; + return [newTextCopy isEqualToAttributedString:oldTextCopy]; } return [self markdown__textOf:newText equals:oldText];