Skip to content

Commit

Permalink
Fix applying underline to the whole text on singleline input blur
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw committed Dec 10, 2024
1 parent 0c381f6 commit 1363312
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apple/MarkdownTextFieldObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ NS_ASSUME_NONNULL_BEGIN

- (void)textFieldDidChange:(UITextField *)textField;

- (void)textFieldDidEndEditing:(UITextField *)textField;

@end

NS_ASSUME_NONNULL_END
14 changes: 14 additions & 0 deletions apple/MarkdownTextFieldObserver.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ - (void)textFieldDidChange:(__unused UITextField *)textField {
[_textField setSelectedTextRange:textRange notifyDelegate:NO];
}

- (void)textFieldDidEndEditing:(__unused UITextField *)textField
{
// In order to prevent iOS from applying underline to the whole text if text ends with a link on blur,
// we need to update `defaultTextAttributes` which at this point doesn't contain NSUnderline attribute yet.
// It seems like the setter performs deep comparision, so we differentiate the new value using a counter,
// otherwise this trick would work only once.
static NSAttributedStringKey RCTLiveMarkdownForceUpdateAttributeName = @"RCTLiveMarkdownForceUpdate";
static NSUInteger counter = 0;
NSMutableDictionary *defaultTextAttributes = [_textField.defaultTextAttributes mutableCopy];
defaultTextAttributes[RCTLiveMarkdownForceUpdateAttributeName] = @(counter++);
_textField.defaultTextAttributes = defaultTextAttributes;
[self textFieldDidChange:_textField];
}

@end
2 changes: 2 additions & 0 deletions apple/MarkdownTextInputDecoratorView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ - (void)didMoveToWindow {

// register observers for future edits
[_textField addTarget:_markdownTextFieldObserver action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[_textField addTarget:_markdownTextFieldObserver action:@selector(textFieldDidEndEditing:) forControlEvents:UIControlEventEditingDidEnd];
[_textField addObserver:_markdownTextFieldObserver forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:NULL];
[_textField addObserver:_markdownTextFieldObserver forKeyPath:@"attributedText" options:NSKeyValueObservingOptionNew context:NULL];

Expand Down Expand Up @@ -132,6 +133,7 @@ - (void)willMoveToWindow:(UIWindow *)newWindow

if (_textField != nil) {
[_textField removeTarget:_markdownTextFieldObserver action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[_textField removeTarget:_markdownTextFieldObserver action:@selector(textFieldDidEndEditing:) forControlEvents:UIControlEventEditingDidEnd];
[_textField removeObserver:_markdownTextFieldObserver forKeyPath:@"text" context:NULL];
[_textField removeObserver:_markdownTextFieldObserver forKeyPath:@"attributedText" context:NULL];
_markdownTextFieldObserver = nil;
Expand Down

0 comments on commit 1363312

Please sign in to comment.