Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <react/renderer/attributedstring/conversions.h>
#include <react/renderer/core/RawValue.h>
#include <react/renderer/graphics/Color.h>

using namespace facebook::react;

Expand Down Expand Up @@ -45,6 +46,70 @@ - (void)testTextAlignmentStartAndEndResolveWithLayoutDirection
XCTAssertEqual(NSTextAlignmentFromTextAlign(@"end", LayoutDirection::RightToLeft), NSTextAlignmentLeft);
}

- (void)testDefaultClearBackgroundIsOmitted
{
TextAttributes textAttributes = TextAttributes::defaultTextAttributes();

NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes);

XCTAssertNil(attributes[NSBackgroundColorAttributeName]);
XCTAssertEqualObjects(attributes[NSForegroundColorAttributeName], [UIColor colorWithRed:0 green:0 blue:0 alpha:1]);
}

- (void)testOpacityDoesNotAddBackgroundColor
{
TextAttributes textAttributes;
textAttributes.opacity = 0.5;

NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes);

XCTAssertNil(attributes[NSBackgroundColorAttributeName]);
XCTAssertEqualObjects(attributes[NSForegroundColorAttributeName], [UIColor colorWithWhite:0 alpha:0.5]);
}

- (void)testDefaultClearBackgroundWithOpacityIsOmitted
{
TextAttributes textAttributes = TextAttributes::defaultTextAttributes();
textAttributes.opacity = 0.5;

NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes);

XCTAssertNil(attributes[NSBackgroundColorAttributeName]);
XCTAssertEqualObjects(attributes[NSForegroundColorAttributeName], [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]);
}

- (void)testVisibleBackgroundColorPreservesAlphaWithOpacity
{
TextAttributes textAttributes;
textAttributes.backgroundColor = colorFromComponents({.red = 1, .alpha = 0.5, .colorSpace = ColorSpace::sRGB});

NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes);

XCTAssertEqualObjects(attributes[NSBackgroundColorAttributeName], [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5]);

textAttributes.opacity = 0.5;
attributes = RCTNSTextAttributesFromTextAttributes(textAttributes);

XCTAssertEqualObjects(attributes[NSBackgroundColorAttributeName], [UIColor colorWithRed:1 green:0 blue:0 alpha:0.25]);
}

- (void)testForegroundColorPreservesAlphaWithOpacity
{
TextAttributes textAttributes = TextAttributes::defaultTextAttributes();
textAttributes.foregroundColor = colorFromComponents({.blue = 1, .alpha = 0.5, .colorSpace = ColorSpace::sRGB});

NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes);

XCTAssertNil(attributes[NSBackgroundColorAttributeName]);
XCTAssertEqualObjects(attributes[NSForegroundColorAttributeName], [UIColor colorWithRed:0 green:0 blue:1 alpha:0.5]);

textAttributes.opacity = 0.5;
attributes = RCTNSTextAttributesFromTextAttributes(textAttributes);

XCTAssertNil(attributes[NSBackgroundColorAttributeName]);
XCTAssertEqualObjects(attributes[NSForegroundColorAttributeName], [UIColor colorWithRed:0 green:0 blue:1 alpha:0.25]);
}

- (void)testSamenessOfEmptyAttributedStrings
{
NSAttributedString *attributedString1 = [[NSAttributedString alloc] initWithString:@""];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
attributes[NSForegroundColorAttributeName] = effectiveForegroundColor;
}

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

Expand Down
Loading