From 6d45f5adabc6d4d87a4fda93c1de56395ca32401 Mon Sep 17 00:00:00 2001 From: giomfo Date: Wed, 28 Sep 2016 13:20:57 +0200 Subject: [PATCH] Bug Fix - Room message search : the search pattern is not highlighted in results https://github.com/vector-im/vector-ios/issues/660 --- MatrixKit/Models/Room/MXKRoomBubbleCellData.h | 7 ++ MatrixKit/Models/Room/MXKRoomBubbleCellData.m | 68 +++++++++++++++++++ .../Room/MXKRoomBubbleCellDataStoring.h | 9 +++ .../MXKRoomBubbleCellDataWithAppendingMode.m | 2 +- 4 files changed, 85 insertions(+), 1 deletion(-) diff --git a/MatrixKit/Models/Room/MXKRoomBubbleCellData.h b/MatrixKit/Models/Room/MXKRoomBubbleCellData.h index 055e5a435..f02fcceee 100644 --- a/MatrixKit/Models/Room/MXKRoomBubbleCellData.h +++ b/MatrixKit/Models/Room/MXKRoomBubbleCellData.h @@ -44,6 +44,13 @@ The body of the message with sets of attributes, or kind of content description in case of attachment (e.g. "image attachment") */ NSAttributedString *attributedTextMessage; + + /** + The optional text pattern to be highlighted in the body of the message. + */ + NSString *highlightedPattern; + UIColor *highlightedPatternColor; + UIFont *highlightedPatternFont; } /** diff --git a/MatrixKit/Models/Room/MXKRoomBubbleCellData.m b/MatrixKit/Models/Room/MXKRoomBubbleCellData.m index 6afdbbb91..ceb2d7fc4 100644 --- a/MatrixKit/Models/Room/MXKRoomBubbleCellData.m +++ b/MatrixKit/Models/Room/MXKRoomBubbleCellData.m @@ -255,6 +255,16 @@ - (NSAttributedString*)attributedTextMessageWithHighlightedEvent:(NSString*)even return customAttributedTextMsg; } +- (void)highlightPatternInTextMessage:(NSString*)pattern withForegroundColor:(UIColor*)patternColor andFont:(UIFont*)patternFont +{ + highlightedPattern = pattern; + highlightedPatternColor = patternColor; + highlightedPatternFont = patternFont; + + // flush the current attributed string to force refresh + self.attributedTextMessage = nil; +} + #pragma mark - - (void)prepareBubbleComponentsPosition @@ -361,6 +371,11 @@ - (void)setAttributedTextMessage:(NSAttributedString *)inAttributedTextMessage { attributedTextMessage = inAttributedTextMessage; + if (attributedTextMessage.length && highlightedPattern) + { + [self highlightPattern]; + } + // Reset content size _contentSize = CGSizeZero; } @@ -375,6 +390,11 @@ - (NSAttributedString*)attributedTextMessage if (firstComponent) { attributedTextMessage = firstComponent.attributedTextMessage; + + if (attributedTextMessage.length && highlightedPattern) + { + [self highlightPattern]; + } } } @@ -549,4 +569,52 @@ - (MXKEventFormatter *)eventFormatter return nil; } +#pragma mark - Internals + +- (void)highlightPattern +{ + NSMutableAttributedString *customAttributedTextMsg = nil; + + NSString *textMessage = self.textMessage; + NSRange range = [textMessage rangeOfString:highlightedPattern options:NSCaseInsensitiveSearch]; + + if (range.location != NSNotFound) + { + customAttributedTextMsg = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedTextMessage]; + + while (range.location != NSNotFound) + { + if (highlightedPatternColor) + { + // Update text color + [customAttributedTextMsg addAttribute:NSForegroundColorAttributeName value:highlightedPatternColor range:range]; + } + + if (highlightedPatternFont) + { + // Update text font + [customAttributedTextMsg addAttribute:NSFontAttributeName value:highlightedPatternFont range:range]; + } + + // Look for the next pattern occurrence + range.location += range.length; + if (range.location < textMessage.length) + { + range.length = textMessage.length - range.location; + range = [textMessage rangeOfString:highlightedPattern options:NSCaseInsensitiveSearch range:range]; + } + else + { + range.location = NSNotFound; + } + } + } + + if (customAttributedTextMsg) + { + // Update resulting message body + attributedTextMessage = customAttributedTextMsg; + } +} + @end diff --git a/MatrixKit/Models/Room/MXKRoomBubbleCellDataStoring.h b/MatrixKit/Models/Room/MXKRoomBubbleCellDataStoring.h index 7b5a5e68a..9f8052a6a 100644 --- a/MatrixKit/Models/Room/MXKRoomBubbleCellDataStoring.h +++ b/MatrixKit/Models/Room/MXKRoomBubbleCellDataStoring.h @@ -190,6 +190,15 @@ Update the event because its mxkState changed or it is has been redacted. */ - (NSAttributedString*)attributedTextMessageWithHighlightedEvent:(NSString*)eventId tintColor:(UIColor*)tintColor; +/** + Highlight all the occurrences of a pattern in the resulting message body 'attributedTextMessage'. + + @param pattern the text pattern to highlight. + @param patternColor optional text color (the pattern text color is unchanged if nil). + @param patternFont optional text font (the pattern font is unchanged if nil). + */ +- (void)highlightPatternInTextMessage:(NSString*)pattern withForegroundColor:(UIColor*)patternColor andFont:(UIFont*)patternFont; + @optional /** Attempt to add a new event to the bubble. diff --git a/MatrixKit/Models/Room/MXKRoomBubbleCellDataWithAppendingMode.m b/MatrixKit/Models/Room/MXKRoomBubbleCellDataWithAppendingMode.m index 6f9137c3f..56ffd2953 100644 --- a/MatrixKit/Models/Room/MXKRoomBubbleCellDataWithAppendingMode.m +++ b/MatrixKit/Models/Room/MXKRoomBubbleCellDataWithAppendingMode.m @@ -251,7 +251,7 @@ - (NSAttributedString*)attributedTextMessage [currentAttributedTextMsg appendAttributedString:component.attributedTextMessage]; } } - attributedTextMessage = currentAttributedTextMsg; + self.attributedTextMessage = currentAttributedTextMsg; } }