diff --git a/android/src/main/java/com/swmansion/enriched/styles/HtmlStyle.kt b/android/src/main/java/com/swmansion/enriched/styles/HtmlStyle.kt index d93261ed0..8a4b9088f 100644 --- a/android/src/main/java/com/swmansion/enriched/styles/HtmlStyle.kt +++ b/android/src/main/java/com/swmansion/enriched/styles/HtmlStyle.kt @@ -154,6 +154,8 @@ class HtmlStyle { } private fun withOpacity(color: Int, alpha: Int): Int { + // Do not apply opacity to transparent color + if (Color.alpha(color) == 0) return color val a = alpha.coerceIn(0, 255) return (color and 0x00FFFFFF) or (a shl 24) } diff --git a/ios/styles/InlineCodeStyle.mm b/ios/styles/InlineCodeStyle.mm index b0cdd8070..15d91311b 100644 --- a/ios/styles/InlineCodeStyle.mm +++ b/ios/styles/InlineCodeStyle.mm @@ -3,6 +3,7 @@ #import "FontExtension.h" #import "OccurenceUtils.h" #import "ParagraphsUtils.h" +#import "ColorExtension.h" @implementation InlineCodeStyle { EnrichedTextInputView *_input; @@ -33,7 +34,7 @@ - (void)addAttributes:(NSRange)range { NSRange currentRange = [value rangeValue]; [_input->textView.textStorage beginEditing]; - [_input->textView.textStorage addAttribute:NSBackgroundColorAttributeName value:[[_input->config inlineCodeBgColor] colorWithAlphaComponent:0.4] range:currentRange]; + [_input->textView.textStorage addAttribute:NSBackgroundColorAttributeName value:[[_input->config inlineCodeBgColor] colorWithAlphaIfNotTransparent:0.4] range:currentRange]; [_input->textView.textStorage addAttribute:NSForegroundColorAttributeName value:[_input->config inlineCodeFgColor] range:currentRange]; [_input->textView.textStorage addAttribute:NSUnderlineColorAttributeName value:[_input->config inlineCodeFgColor] range:currentRange]; [_input->textView.textStorage addAttribute:NSStrikethroughColorAttributeName value:[_input->config inlineCodeFgColor] range:currentRange]; @@ -53,7 +54,7 @@ - (void)addAttributes:(NSRange)range { - (void)addTypingAttributes { NSMutableDictionary *newTypingAttrs = [_input->textView.typingAttributes mutableCopy]; - newTypingAttrs[NSBackgroundColorAttributeName] = [[_input->config inlineCodeBgColor] colorWithAlphaComponent:0.4]; + newTypingAttrs[NSBackgroundColorAttributeName] = [[_input->config inlineCodeBgColor] colorWithAlphaIfNotTransparent:0.4]; newTypingAttrs[NSForegroundColorAttributeName] = [_input->config inlineCodeFgColor]; newTypingAttrs[NSUnderlineColorAttributeName] = [_input->config inlineCodeFgColor]; newTypingAttrs[NSStrikethroughColorAttributeName] = [_input->config inlineCodeFgColor]; diff --git a/ios/styles/MentionStyle.mm b/ios/styles/MentionStyle.mm index 673106a4d..f0ec496a3 100644 --- a/ios/styles/MentionStyle.mm +++ b/ios/styles/MentionStyle.mm @@ -4,6 +4,7 @@ #import "TextInsertionUtils.h" #import "WordsUtils.h" #import "UIView+React.h" +#import "ColorExtension.h" // custom NSAttributedStringKey to differentiate from links static NSString *const MentionAttributeName = @"MentionAttributeName"; @@ -154,7 +155,7 @@ - (void)addMention:(NSString *)indicator text:(NSString *)text attributes:(NSStr NSForegroundColorAttributeName: styleProps.color, NSUnderlineColorAttributeName: styleProps.color, NSStrikethroughColorAttributeName: styleProps.color, - NSBackgroundColorAttributeName: [styleProps.backgroundColor colorWithAlphaComponent:0.4], + NSBackgroundColorAttributeName: [styleProps.backgroundColor colorWithAlphaIfNotTransparent:0.4], } mutableCopy]; if(styleProps.decorationLine == DecorationUnderline) { @@ -186,7 +187,7 @@ - (void)addMentionAtRange:(NSRange)range params:(MentionParams *)params { NSForegroundColorAttributeName: styleProps.color, NSUnderlineColorAttributeName: styleProps.color, NSStrikethroughColorAttributeName: styleProps.color, - NSBackgroundColorAttributeName: [styleProps.backgroundColor colorWithAlphaComponent:0.4], + NSBackgroundColorAttributeName: [styleProps.backgroundColor colorWithAlphaIfNotTransparent:0.4], } mutableCopy]; if(styleProps.decorationLine == DecorationUnderline) { diff --git a/ios/utils/ColorExtension.h b/ios/utils/ColorExtension.h index e5c242da8..1ed38519a 100644 --- a/ios/utils/ColorExtension.h +++ b/ios/utils/ColorExtension.h @@ -3,4 +3,5 @@ @interface UIColor (ColorExtension) - (BOOL)isEqualToColor:(UIColor *)otherColor; +- (UIColor *)colorWithAlphaIfNotTransparent:(CGFloat)newAlpha; @end diff --git a/ios/utils/ColorExtension.mm b/ios/utils/ColorExtension.mm index eae93f988..cc570142d 100644 --- a/ios/utils/ColorExtension.mm +++ b/ios/utils/ColorExtension.mm @@ -24,4 +24,13 @@ - (BOOL)isEqualToColor:(UIColor *)otherColor { return [selfColor isEqual:otherColor]; } + +- (UIColor *)colorWithAlphaIfNotTransparent:(CGFloat)newAlpha { + CGFloat alpha = 0.0; + [self getRed:nil green:nil blue:nil alpha:&alpha]; + if (alpha > 0.0) { + return [self colorWithAlphaComponent:newAlpha]; + } + return self; +} @end