Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #173 from matrix-org/vector_660
Browse files Browse the repository at this point in the history
Bug Fix - Room message search : the search pattern is not highlighted…
  • Loading branch information
giomfo authored Sep 28, 2016
2 parents 485cd8a + 6d45f5a commit 0f6dc9a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
7 changes: 7 additions & 0 deletions MatrixKit/Models/Room/MXKRoomBubbleCellData.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
68 changes: 68 additions & 0 deletions MatrixKit/Models/Room/MXKRoomBubbleCellData.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -361,6 +371,11 @@ - (void)setAttributedTextMessage:(NSAttributedString *)inAttributedTextMessage
{
attributedTextMessage = inAttributedTextMessage;

if (attributedTextMessage.length && highlightedPattern)
{
[self highlightPattern];
}

// Reset content size
_contentSize = CGSizeZero;
}
Expand All @@ -375,6 +390,11 @@ - (NSAttributedString*)attributedTextMessage
if (firstComponent)
{
attributedTextMessage = firstComponent.attributedTextMessage;

if (attributedTextMessage.length && highlightedPattern)
{
[self highlightPattern];
}
}
}

Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions MatrixKit/Models/Room/MXKRoomBubbleCellDataStoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ - (NSAttributedString*)attributedTextMessage
[currentAttributedTextMsg appendAttributedString:component.attributedTextMessage];
}
}
attributedTextMessage = currentAttributedTextMsg;
self.attributedTextMessage = currentAttributedTextMsg;
}
}

Expand Down

0 comments on commit 0f6dc9a

Please # to comment.