Skip to content

Commit

Permalink
Reset font and color after bleaching mentions (#211)
Browse files Browse the repository at this point in the history
Restore font/color after bleaching mentions

+ small fix Add delegate call for text view changing to paste
  • Loading branch information
bkoatz authored Nov 6, 2020
1 parent aa48147 commit 0799210
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Hakawai/Core/HKWTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ - (void)paste:(id)sender {
[string replaceCharactersInRange:selectionRangeBeforePaste withAttributedString:copyString];
[self setAttributedText:string];
self.selectedRange = NSMakeRange(cursorLocationAfterPaste, 0);
// Inform delegate that text view has changed since we are overriding the normal paste behavior that would do so automatically
[self.delegate textViewDidChange:self];
} else {
[super paste:sender];
}
Expand Down
52 changes: 26 additions & 26 deletions Hakawai/Mentions/HKWMentionsPluginV2.m
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,19 @@ - (BOOL)stringValidForMentionsCreation:(NSString *)string {
return YES;
}

- (NSDictionary *)defaultTextAttributes {
__strong __auto_type parentTextView = self.parentTextView;
NSMutableDictionary *returnDict = [[NSMutableDictionary alloc] init];
UIFont *parentFont = parentTextView.fontSetByApp;
UIColor *parentColor = parentTextView.textColorSetByApp;
if (parentFont) {
returnDict[NSFontAttributeName] = parentFont;
}
if (parentColor) {
returnDict[NSForegroundColorAttributeName] = parentColor;
}
return returnDict;
}
/*!
Build a new typing attributes dictionary by stripping mentions-specific attributes from an original attributes
dictionary and, if applicable, restoring default attributes from the parent text view.
Expand All @@ -366,14 +379,8 @@ - (NSDictionary *)typingAttributesByStrippingMentionAttributes:(NSDictionary *)o
for (NSString *key in self.mentionUnselectedAttributes) {
[d removeObjectForKey:key];
}
// Restore the font and/or text color, if the app set either explicitly at any point.
__strong __auto_type parentTextView = self.parentTextView;
if (parentTextView.fontSetByApp) {
d[NSFontAttributeName] = parentTextView.fontSetByApp;
}
if (parentTextView.textColorSetByApp) {
d[NSForegroundColorAttributeName] = parentTextView.textColorSetByApp;
}
// Restore the default typing attributes, if the app set either explicitly at any point.
[d addEntriesFromDictionary:self.defaultTextAttributes];
return d;
}

Expand Down Expand Up @@ -471,23 +478,8 @@ - (NSUInteger)bleachMentionsWithinRange:(NSRange)bleachRange {
[ranges addObject:[NSValue valueWithRange:range]];
}
}];
NSDictionary *selectedAttributes = self.mentionSelectedAttributes;
NSDictionary *unselectedAttributes = self.mentionUnselectedAttributes;
for (NSValue *v in ranges) {
[parentTextView transformTextAtRange:[v rangeValue]
withTransformer:^NSAttributedString *(NSAttributedString *input) {
NSMutableAttributedString *buffer = [input mutableCopy];
[buffer removeAttribute:HKWMentionAttributeName range:HKW_FULL_RANGE(input)];
// NOTE: We may need to add support for capturing and restoring any attributes
// overwritten by applying the special mentions attributes in the future.
for (NSString *key in unselectedAttributes) {
[buffer removeAttribute:key range:HKW_FULL_RANGE(input)];
}
for (NSString *key in selectedAttributes) {
[buffer removeAttribute:key range:HKW_FULL_RANGE(input)];
}
return [buffer copy];
}];
[self stripMentionAttributesAtRange:[v rangeValue]];
}
// Restore previously selected range
parentTextView.selectedRange = previousSelectedRange;
Expand Down Expand Up @@ -521,7 +513,13 @@ - (void)bleachExistingMentionAtRange:(NSRange)range {
(unsigned long)range.location, (unsigned long)range.length, (unsigned long)dataRange.location,
(unsigned long)dataRange.length);
#endif
[self stripMentionAttributesAtRange:range];
// Restore previously selected range
parentTextView.selectedRange = previousSelectedRange;
}
- (void)stripMentionAttributesAtRange:(NSRange)range {
__strong __auto_type parentTextView = self.parentTextView;
NSDictionary *unselectedAttributes = self.mentionUnselectedAttributes;
NSDictionary *selectedAttributes = self.mentionSelectedAttributes;
[parentTextView transformTextAtRange:range withTransformer:^NSAttributedString *(NSAttributedString *input) {
Expand All @@ -535,10 +533,12 @@ - (void)bleachExistingMentionAtRange:(NSRange)range {
for (NSString *key in unselectedAttributes) {
[buffer removeAttribute:key range:HKW_FULL_RANGE(input)];
}
// Restore default attributes to text
for (NSString *key in self.defaultTextAttributes) {
[buffer addAttribute:key value:self.defaultTextAttributes[key] range:HKW_FULL_RANGE(input)];
}
return [buffer copy];
}];
// Restore previously selected range
parentTextView.selectedRange = previousSelectedRange;
}
/*!
Expand Down

0 comments on commit 0799210

Please sign in to comment.