Skip to content

Commit

Permalink
Fix selection issue for text containing emoji (Expensify#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
QichenZhu authored and hannojg committed Jul 17, 2024
1 parent 8c56884 commit cca4d18
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion ios/RCTBaseTextInputView+Markdown.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
9 changes: 8 additions & 1 deletion ios/RCTTextInputComponentView+Markdown.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit cca4d18

Please sign in to comment.