diff --git a/CHANGES.rst b/CHANGES.rst index 7bfb8a399..822fa5f7f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -9,7 +9,8 @@ Improvements: * Room settings: Anyone can now set a room alias (#2033). Bug fix: -* Fix missing read receipts when lazy-loading room members. +* Fix missing read receipts when lazy-loading room members.] +* Weird text color when selecting a message (#2046). Changes in 0.7.3 (2018-08-27) =============================================== diff --git a/Riot/Modules/Room/CellData/RoomBubbleCellData.m b/Riot/Modules/Room/CellData/RoomBubbleCellData.m index 23e434e31..55aa3051e 100644 --- a/Riot/Modules/Room/CellData/RoomBubbleCellData.m +++ b/Riot/Modules/Room/CellData/RoomBubbleCellData.m @@ -19,6 +19,7 @@ #import "EventFormatter.h" #import "AvatarGenerator.h" +#import "Tools.h" static NSAttributedString *timestampVerticalWhitespace = nil; static NSAttributedString *readReceiptVerticalWhitespace = nil; @@ -194,12 +195,7 @@ static NSAttributedString *readReceiptVerticalWhitespace = nil; if (selectedComponentIndex != NSNotFound && selectedComponentIndex != index && componentString.length) { // Apply alpha to blur this component - NSMutableAttributedString *customComponentString = [[NSMutableAttributedString alloc] initWithAttributedString:componentString]; - UIColor *color = [componentString attribute:NSForegroundColorAttributeName atIndex:0 effectiveRange:nil]; - color = [color colorWithAlphaComponent:0.2]; - - [customComponentString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, customComponentString.length)]; - componentString = customComponentString; + componentString = [Tools setTextColorAlpha:.2 inAttributedString:componentString]; } // Check whether the timestamp is displayed for this component, and check whether a vertical whitespace is required @@ -238,12 +234,7 @@ static NSAttributedString *readReceiptVerticalWhitespace = nil; if (selectedComponentIndex != NSNotFound && selectedComponentIndex != index && componentString.length) { // Apply alpha to blur this component - NSMutableAttributedString *customComponentString = [[NSMutableAttributedString alloc] initWithAttributedString:componentString]; - UIColor *color = [componentString attribute:NSForegroundColorAttributeName atIndex:0 effectiveRange:nil]; - color = [color colorWithAlphaComponent:0.2]; - - [customComponentString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, customComponentString.length)]; - componentString = customComponentString; + componentString = [Tools setTextColorAlpha:.2 inAttributedString:componentString]; } // Check whether the timestamp is displayed diff --git a/Riot/Utils/Tools.h b/Riot/Utils/Tools.h index 434f782e1..3239f3e04 100644 --- a/Riot/Utils/Tools.h +++ b/Riot/Utils/Tools.h @@ -54,4 +54,15 @@ */ + (NSURL*)fixURLWithSeveralHashKeys:(NSURL*)url; +#pragma mark - String utilities + +/** + Change the alpha value of all text colors of an attibuted string. + + @param alpha the alpha value to apply. + @param attributedString the attributed string to update. + @return a new attributed string. + */ ++ (NSAttributedString *)setTextColorAlpha:(CGFloat)alpha inAttributedString:(NSAttributedString*)attributedString; + @end diff --git a/Riot/Utils/Tools.m b/Riot/Utils/Tools.m index 6855a4ade..842ae1f3f 100644 --- a/Riot/Utils/Tools.m +++ b/Riot/Utils/Tools.m @@ -117,4 +117,29 @@ return fixedURL; } +#pragma mark - String utilities + ++ (NSAttributedString *)setTextColorAlpha:(CGFloat)alpha inAttributedString:(NSAttributedString*)attributedString +{ + NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString]; + + // Check all attributes one by one + [string enumerateAttributesInRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) + { + // Replace only colored texts + if (attrs[NSForegroundColorAttributeName]) + { + UIColor *color = attrs[NSForegroundColorAttributeName]; + color = [color colorWithAlphaComponent:0.2]; + + NSMutableDictionary *newAttrs = [NSMutableDictionary dictionaryWithDictionary:attrs]; + newAttrs[NSForegroundColorAttributeName] = color; + + [string setAttributes:newAttrs range:range]; + } + }]; + + return string; +} + @end