Skip to content

Commit dbeb062

Browse files
committed
fix(ios): backport clear text background omission to 0.87
1 parent 2a41d8c commit dbeb062

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎packages/react-native/React/Tests/Text/RCTAttributedTextUtilsTest.mm‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <react/renderer/attributedstring/conversions.h>
1414
#include <react/renderer/core/RawValue.h>
15+
#include <react/renderer/graphics/Color.h>
1516

1617
using namespace facebook::react;
1718

@@ -45,6 +46,70 @@ - (void)testTextAlignmentStartAndEndResolveWithLayoutDirection
4546
XCTAssertEqual(NSTextAlignmentFromTextAlign(@"end", LayoutDirection::RightToLeft), NSTextAlignmentLeft);
4647
}
4748

49+
- (void)testDefaultClearBackgroundIsOmitted
50+
{
51+
TextAttributes textAttributes = TextAttributes::defaultTextAttributes();
52+
53+
NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes);
54+
55+
XCTAssertNil(attributes[NSBackgroundColorAttributeName]);
56+
XCTAssertEqualObjects(attributes[NSForegroundColorAttributeName], [UIColor colorWithRed:0 green:0 blue:0 alpha:1]);
57+
}
58+
59+
- (void)testOpacityDoesNotAddBackgroundColor
60+
{
61+
TextAttributes textAttributes;
62+
textAttributes.opacity = 0.5;
63+
64+
NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes);
65+
66+
XCTAssertNil(attributes[NSBackgroundColorAttributeName]);
67+
XCTAssertEqualObjects(attributes[NSForegroundColorAttributeName], [UIColor colorWithWhite:0 alpha:0.5]);
68+
}
69+
70+
- (void)testDefaultClearBackgroundWithOpacityIsOmitted
71+
{
72+
TextAttributes textAttributes = TextAttributes::defaultTextAttributes();
73+
textAttributes.opacity = 0.5;
74+
75+
NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes);
76+
77+
XCTAssertNil(attributes[NSBackgroundColorAttributeName]);
78+
XCTAssertEqualObjects(attributes[NSForegroundColorAttributeName], [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]);
79+
}
80+
81+
- (void)testVisibleBackgroundColorPreservesAlphaWithOpacity
82+
{
83+
TextAttributes textAttributes;
84+
textAttributes.backgroundColor = colorFromComponents({.red = 1, .alpha = 0.5, .colorSpace = ColorSpace::sRGB});
85+
86+
NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes);
87+
88+
XCTAssertEqualObjects(attributes[NSBackgroundColorAttributeName], [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5]);
89+
90+
textAttributes.opacity = 0.5;
91+
attributes = RCTNSTextAttributesFromTextAttributes(textAttributes);
92+
93+
XCTAssertEqualObjects(attributes[NSBackgroundColorAttributeName], [UIColor colorWithRed:1 green:0 blue:0 alpha:0.25]);
94+
}
95+
96+
- (void)testForegroundColorPreservesAlphaWithOpacity
97+
{
98+
TextAttributes textAttributes = TextAttributes::defaultTextAttributes();
99+
textAttributes.foregroundColor = colorFromComponents({.blue = 1, .alpha = 0.5, .colorSpace = ColorSpace::sRGB});
100+
101+
NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes);
102+
103+
XCTAssertNil(attributes[NSBackgroundColorAttributeName]);
104+
XCTAssertEqualObjects(attributes[NSForegroundColorAttributeName], [UIColor colorWithRed:0 green:0 blue:1 alpha:0.5]);
105+
106+
textAttributes.opacity = 0.5;
107+
attributes = RCTNSTextAttributesFromTextAttributes(textAttributes);
108+
109+
XCTAssertNil(attributes[NSBackgroundColorAttributeName]);
110+
XCTAssertEqualObjects(attributes[NSForegroundColorAttributeName], [UIColor colorWithRed:0 green:0 blue:1 alpha:0.25]);
111+
}
112+
48113
- (void)testSamenessOfEmptyAttributedStrings
49114
{
50115
NSAttributedString *attributedString1 = [[NSAttributedString alloc] initWithString:@""];

‎packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
200200
attributes[NSForegroundColorAttributeName] = effectiveForegroundColor;
201201
}
202202

203-
if (textAttributes.backgroundColor || !isnan(textAttributes.opacity)) {
203+
// A clear background draws nothing, but TextKit still computes and fills its rects on every draw.
204+
if (textAttributes.backgroundColor && textAttributes.backgroundColor != clearColor()) {
204205
attributes[NSBackgroundColorAttributeName] = RCTEffectiveBackgroundColorFromTextAttributes(textAttributes);
205206
}
206207

0 commit comments

Comments
 (0)