diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.h b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.h index 902912e6f20..6ceec3a0167 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.h +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.h @@ -19,6 +19,17 @@ NSString *const RCTAttributedStringEventEmitterKey = @"EventEmitter"; // String representation of either `role` or `accessibilityRole` NSString *const RCTTextAttributesAccessibilityRoleAttributeName = @"AccessibilityRole"; +// Custom attribute key for ranges whose decoration line cannot be rendered +// faithfully via UIKit's `NSUnderlineStyle` pattern bits (wavy has no native +// equivalent; dotted/dashed don't match the geometry browsers use). These +// ranges are painted by `RCTTextLayoutManager`'s drawing pass. +// +// Stored as an NSDictionary: +// @"lines": NSArray of @"underline" / @"line-through" +// @"color": UIColor stroke color +// @"style": NSString — @"wavy" | @"dotted" | @"dashed" +NSString *const RCTCustomDecorationAttributeName = @"RCTCustomDecoration"; + /* * Creates `NSTextAttributes` from given `facebook::react::TextAttributes` */ diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm index f96a0494000..f2e125970d6 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm @@ -240,29 +240,56 @@ NSMutableDictionary *RCTNSTextAttributesFromTextAttri // Decoration if (textAttributes.textDecorationLineType.value_or(TextDecorationLineType::None) != TextDecorationLineType::None) { auto textDecorationLineType = textAttributes.textDecorationLineType.value(); - - NSUnderlineStyle style = RCTNSUnderlineStyleFromTextDecorationStyle( - textAttributes.textDecorationStyle.value_or(TextDecorationStyle::Solid)); - + auto textDecorationStyleValue = textAttributes.textDecorationStyle.value_or(TextDecorationStyle::Solid); UIColor *textDecorationColor = RCTUIColorFromSharedColor(textAttributes.textDecorationColor); - // Underline - if (textDecorationLineType == TextDecorationLineType::Underline || - textDecorationLineType == TextDecorationLineType::UnderlineStrikethrough) { - attributes[NSUnderlineStyleAttributeName] = @(style); - - if (textDecorationColor) { - attributes[NSUnderlineColorAttributeName] = textDecorationColor; + // Custom drawing for styles UIKit can't render faithfully: wavy (no + // native value), and dotted/dashed (UIKit's pattern bits don't match + // browser geometry). The other styles continue to use NSUnderlineStyle. + bool needsCustomDrawing = textDecorationStyleValue == TextDecorationStyle::Wavy || + textDecorationStyleValue == TextDecorationStyle::Dotted || + textDecorationStyleValue == TextDecorationStyle::Dashed; + if (needsCustomDrawing) { + UIColor *strokeColor = (textDecorationColor != nil) ? textDecorationColor + : RCTUIColorFromSharedColor(textAttributes.foregroundColor); + NSMutableArray *lines = [NSMutableArray array]; + if (textDecorationLineType == TextDecorationLineType::Underline || + textDecorationLineType == TextDecorationLineType::UnderlineStrikethrough) { + [lines addObject:@"underline"]; } - } + if (textDecorationLineType == TextDecorationLineType::Strikethrough || + textDecorationLineType == TextDecorationLineType::UnderlineStrikethrough) { + [lines addObject:@"line-through"]; + } + NSString *styleKey = textDecorationStyleValue == TextDecorationStyle::Wavy + ? @"wavy" + : (textDecorationStyleValue == TextDecorationStyle::Dotted ? @"dotted" : @"dashed"); + attributes[RCTCustomDecorationAttributeName] = @{ + @"lines" : lines, + @"color" : (strokeColor != nil) ? strokeColor : [UIColor labelColor], + @"style" : styleKey + }; + } else { + NSUnderlineStyle style = RCTNSUnderlineStyleFromTextDecorationStyle(textDecorationStyleValue); - // Strikethrough - if (textDecorationLineType == TextDecorationLineType::Strikethrough || - textDecorationLineType == TextDecorationLineType::UnderlineStrikethrough) { - attributes[NSStrikethroughStyleAttributeName] = @(style); + // Underline + if (textDecorationLineType == TextDecorationLineType::Underline || + textDecorationLineType == TextDecorationLineType::UnderlineStrikethrough) { + attributes[NSUnderlineStyleAttributeName] = @(style); - if (textDecorationColor) { - attributes[NSStrikethroughColorAttributeName] = textDecorationColor; + if (textDecorationColor != nil) { + attributes[NSUnderlineColorAttributeName] = textDecorationColor; + } + } + + // Strikethrough + if (textDecorationLineType == TextDecorationLineType::Strikethrough || + textDecorationLineType == TextDecorationLineType::UnderlineStrikethrough) { + attributes[NSStrikethroughStyleAttributeName] = @(style); + + if (textDecorationColor != nil) { + attributes[NSStrikethroughColorAttributeName] = textDecorationColor; + } } } } diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm index ac553045a9c..1252eb49bea 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm @@ -7,6 +7,8 @@ #import "RCTTextLayoutManager.h" +#import + #import "RCTAttributedTextUtils.h" #import @@ -95,6 +97,137 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(EllipsizeMode ellipsi CGContextRestoreGState(context); #endif + // Custom decoration pass: enumerate `RCTCustomDecorationAttributeName` + // ranges and paint each one ourselves. Covers wavy (no UIKit equivalent), + // dotted, and dashed (UIKit's pattern bits don't match browser geometry). + { + CGContextRef ctx = UIGraphicsGetCurrentContext(); + if (ctx != nullptr) { + NSRange charRange = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:nullptr]; + [textStorage + enumerateAttribute:RCTCustomDecorationAttributeName + inRange:charRange + options:0 + usingBlock:^(NSDictionary *_Nullable attrs, NSRange attrRange, __unused BOOL *stop) { + if (attrs == nil) { + return; + } + NSArray *lines = attrs[@"lines"]; + UIColor *strokeColor = attrs[@"color"]; + NSString *style = attrs[@"style"]; + UIFont *font = [textStorage attribute:NSFontAttributeName + atIndex:attrRange.location + effectiveRange:nullptr]; + if (font == nil || strokeColor == nil || style == nil) { + return; + } + + CGFloat fontSize = font.pointSize; + // Thickness scales with the type size so the decoration + // remains visible at small sizes and proportionate at + // large ones. ~`fontSize / 12` plus a 1.5pt floor. + CGFloat thickness = MAX(fontSize / 12.0f, 1.5f); + CGFloat wavyWavelength = 1.0f + 2.0f * round(2.0f * thickness + 0.5f); + CGFloat wavyCpDistance = 0.5f + round(3.0f * thickness + 0.5f); + + NSRange targetGlyphRange = [layoutManager glyphRangeForCharacterRange:attrRange + actualCharacterRange:nullptr]; + + CGContextSaveGState(ctx); + CGContextSetStrokeColorWithColor(ctx, strokeColor.CGColor); + CGContextSetLineWidth(ctx, thickness); + CGContextSetShouldAntialias(ctx, YES); + + if ([style isEqualToString:@"dotted"]) { + const std::array dotIntervals = {0.0f, thickness * 2.0f}; + CGContextSetLineDash(ctx, 0, dotIntervals.data(), dotIntervals.size()); + CGContextSetLineCap(ctx, kCGLineCapRound); + } else if ([style isEqualToString:@"dashed"]) { + const std::array dashIntervals = {thickness * 2.0f, thickness}; + CGContextSetLineDash(ctx, 0, dashIntervals.data(), dashIntervals.size()); + CGContextSetLineCap(ctx, kCGLineCapButt); + } else { + CGContextSetLineCap(ctx, kCGLineCapRound); + } + + [layoutManager + enumerateLineFragmentsForGlyphRange:targetGlyphRange + usingBlock:^( + CGRect lineRect, + __unused CGRect usedRect, + NSTextContainer *_Nonnull container, + NSRange lineGlyphRange, + __unused BOOL *_Nonnull innerStop) { + NSRange intersection = + NSIntersectionRange(targetGlyphRange, lineGlyphRange); + if (intersection.length == 0) { + return; + } + CGRect firstGlyphRect = [layoutManager + boundingRectForGlyphRange:NSMakeRange(intersection.location, 1) + inTextContainer:container]; + CGRect lastGlyphRect = [layoutManager + boundingRectForGlyphRange:NSMakeRange( + NSMaxRange(intersection) - 1, 1) + inTextContainer:container]; + CGFloat x1 = firstGlyphRect.origin.x + frame.origin.x; + CGFloat x2 = CGRectGetMaxX(lastGlyphRect) + frame.origin.x; + CGFloat baseline = + lineRect.origin.y + font.ascender + frame.origin.y; + + // NOLINTNEXTLINE(cppcoreguidelines-init-variables) + for (NSString *line in lines) { + CGFloat y = 0.0f; + if ([line isEqualToString:@"underline"]) { + if ([style isEqualToString:@"wavy"]) { + y = baseline + 1.0f; + } else { + y = baseline + thickness + 1.0f; + } + } else { + y = baseline - (font.ascender + font.descender) / 2.0f + 1.0f; + } + if ([style isEqualToString:@"wavy"]) { + CGContextSaveGState(ctx); + CGContextClipToRect( + ctx, + CGRectMake( + x1, + y - wavyCpDistance - thickness, + x2 - x1, + 2 * wavyCpDistance + 2 * thickness)); + CGContextBeginPath(ctx); + CGContextMoveToPoint(ctx, x1, y); + CGFloat step = wavyWavelength / 2.0f; + CGFloat wx = x1; + while (wx < x2) { + CGFloat midX = wx + step; + CGContextAddCurveToPoint( + ctx, + midX, + y + wavyCpDistance, + midX, + y - wavyCpDistance, + wx + wavyWavelength, + y); + wx += wavyWavelength; + } + CGContextStrokePath(ctx); + CGContextRestoreGState(ctx); + } else { + CGContextBeginPath(ctx); + CGContextMoveToPoint(ctx, x1, y); + CGContextAddLineToPoint(ctx, x2, y); + CGContextStrokePath(ctx); + } + } + }]; + + CGContextRestoreGState(ctx); + }]; + } + } + if (block != nil) { __block UIBezierPath *highlightPath = nil; NSRange characterRange = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL]; diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextPrimitivesConversions.h b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextPrimitivesConversions.h index 0ec5e4c5c77..8166d69f919 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextPrimitivesConversions.h +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextPrimitivesConversions.h @@ -106,12 +106,17 @@ inline static NSUnderlineStyle RCTNSUnderlineStyleFromTextDecorationStyle( return NSUnderlineStyleSingle; case facebook::react::TextDecorationStyle::Double: return NSUnderlineStyleDouble; + // Dotted, dashed, and wavy are tagged with + // `RCTCustomDecorationAttributeName` in `RCTAttributedTextUtils.mm` and + // painted by `RCTTextLayoutManager.mm`'s drawing pass; UIKit's pattern + // bits don't match the geometry browsers use, and there is no native + // wavy value at all. These branches are unreachable in normal flow; the + // returned values keep the switch exhaustive. case facebook::react::TextDecorationStyle::Dashed: - return NSUnderlineStylePatternDash | NSUnderlineStyleSingle; + return NSUnderlineStyleSingle; case facebook::react::TextDecorationStyle::Dotted: - return NSUnderlineStylePatternDot | NSUnderlineStyleSingle; + return NSUnderlineStyleSingle; case facebook::react::TextDecorationStyle::Wavy: - // No native NSUnderlineStyle for wavy; fall back to solid. return NSUnderlineStyleSingle; } } diff --git a/packages/rn-tester/js/examples/Text/TextExample.ios.js b/packages/rn-tester/js/examples/Text/TextExample.ios.js index 68b4a103a87..b607c5094c0 100644 --- a/packages/rn-tester/js/examples/Text/TextExample.ios.js +++ b/packages/rn-tester/js/examples/Text/TextExample.ios.js @@ -881,6 +881,14 @@ const examples = [ }}> Dotted underline with custom color + + Wavy underline (red) + None textDecoration Dotted line-through with custom color - - Both underline and line-through + + Wavy line-through (red) + + + Both underline and line-through (wavy) + + + Mixed text with{' '} + underline and{' '} + + line-through + {' '} + text nodes + + + Unstyled text{' '} + + with wavy strike-through that wraps across multiple lines to + verify continuity + {' '} + normal text );