Skip to content
Closed
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
41 changes: 41 additions & 0 deletions packages/react-native/Libraries/Text/Text/RCTTextShadowView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,54 @@ - (NSTextStorage *)textStorageAndLayoutManagerThatFitsSize:(CGSize)size exclusiv
maximumFontSize:self.textAttributes.effectiveFont.pointSize];
}

[self processTruncatedAttributedText:textStorage textContainer:textContainer layoutManager:layoutManager];

if (!exclusiveOwnership) {
[_cachedTextStorages setObject:textStorage forKey:key];
}

return textStorage;
}

- (void)processTruncatedAttributedText:(NSTextStorage *)textStorage
textContainer:(NSTextContainer *)textContainer
layoutManager:(NSLayoutManager *)layoutManager
{
if (_maximumNumberOfLines > 0) {
[layoutManager ensureLayoutForTextContainer:textContainer];
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
__block int line = 0;
[layoutManager
enumerateLineFragmentsForGlyphRange:glyphRange
usingBlock:^(
CGRect rect,
CGRect usedRect,
NSTextContainer *_Nonnull _,
NSRange lineGlyphRange,
BOOL *_Nonnull stop) {
if (line == textContainer.maximumNumberOfLines - 1) {
NSRange truncatedRange = [layoutManager
truncatedGlyphRangeInLineFragmentForGlyphAtIndex:lineGlyphRange.location];

if (truncatedRange.location != NSNotFound && truncatedRange.location >= 1) {
// Remove color attributes for truncated range
for (NSAttributedStringKey key in
@[ NSForegroundColorAttributeName, NSBackgroundColorAttributeName ]) {
[textStorage removeAttribute:key range:truncatedRange];
id attribute = [textStorage attribute:key
atIndex:truncatedRange.location - 1
effectiveRange:nil];
if (attribute) {
[textStorage addAttribute:key value:attribute range:truncatedRange];
}
}
}
}
line++;
}];
}
}

- (void)layoutWithMetrics:(RCTLayoutMetrics)layoutMetrics layoutContext:(RCTLayoutContext)layoutContext
{
// If the view got new `contentFrame`, we have to redraw it because
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ - (void)drawAttributedString:(AttributedString)attributedString
#endif

NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];

[self processTruncatedAttributedText:textStorage textContainer:textContainer layoutManager:layoutManager];

[layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:frame.origin];
[layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:frame.origin];

Expand Down Expand Up @@ -122,6 +125,45 @@ - (void)drawAttributedString:(AttributedString)attributedString
}
}

- (void)processTruncatedAttributedText:(NSTextStorage *)textStorage
textContainer:(NSTextContainer *)textContainer
layoutManager:(NSLayoutManager *)layoutManager
{
if (textContainer.maximumNumberOfLines > 0) {
[layoutManager ensureLayoutForTextContainer:textContainer];
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
__block int line = 0;
[layoutManager
enumerateLineFragmentsForGlyphRange:glyphRange
usingBlock:^(
CGRect rect,
CGRect usedRect,
NSTextContainer *_Nonnull _,
NSRange lineGlyphRange,
BOOL *_Nonnull stop) {
if (line == textContainer.maximumNumberOfLines - 1) {
NSRange truncatedRange = [layoutManager
truncatedGlyphRangeInLineFragmentForGlyphAtIndex:lineGlyphRange.location];

if (truncatedRange.location != NSNotFound && truncatedRange.location >= 1) {
// Remove color attributes for truncated range
for (NSAttributedStringKey key in
@[ NSForegroundColorAttributeName, NSBackgroundColorAttributeName ]) {
[textStorage removeAttribute:key range:truncatedRange];
id attribute = [textStorage attribute:key
atIndex:truncatedRange.location - 1
effectiveRange:nil];
if (attribute) {
[textStorage addAttribute:key value:attribute range:truncatedRange];
}
}
}
}
line++;
}];
}
}

- (LinesMeasurements)getLinesForAttributedString:(facebook::react::AttributedString)attributedString
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
size:(CGSize)size
Expand Down