Skip to content

Commit 70826db

Browse files
Adam Comellafacebook-github-bot
Adam Comella
authored andcommitted
iOS: Support inline view truncation (#21456)
Summary: If text is truncated and an inline view appears after the truncation point, the user should not see the inline view. Instead, we have a bug such that the inline view is always visible at the end of the visible text. This commit fixes this by marking the inline view as hidden if it appears after the truncation point. This appears to be a regression. React Native used to have logic similar to what this commit is adding: https://github.com/facebook/react-native/blob/1e2a924ba60001c6f0587c7561536f00b2922cbf/Libraries/Text/RCTShadowText.m#L186-L192 **Before fix** Inline view (blue square) is visible even though it appears after the truncation point: ![image](https://user-images.githubusercontent.com/199935/46382038-d3a71200-c65d-11e8-8179-2ce4aad8d010.png) The full text being rendered was: ``` <Text numberOfLines={1}> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna <View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} /> </Text> ``` **After fix** Inline view is properly truncated: ![image](https://user-images.githubusercontent.com/199935/46382067-fdf8cf80-c65d-11e8-84ea-e2b71c229dae.png) **Test Plan** Tested that the inline view is hidden if it appears after the truncation point when `numberOfLines` is 1 and 2. Similarly, verified that the inline view is visible if it appears before the truncation point. **Release Notes** [IOS] [BUGFIX] [Text] - Fix case where inline view is visible even though it should have been truncated Adam Comella Microsoft Corp. Pull Request resolved: #21456 Differential Revision: D10182991 Pulled By: shergin fbshipit-source-id: a5bddddb1bb8672b61d4feaa04013a92c8224155
1 parent 27cfba2 commit 70826db

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

Libraries/Text/Text/RCTTextShadowView.m

+7-2
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ - (void)layoutSubviewsWithContext:(RCTLayoutContext)layoutContext
290290
RCTRoundPixelValue(attachmentSize.width),
291291
RCTRoundPixelValue(attachmentSize.height)
292292
}};
293+
294+
NSRange truncatedGlyphRange = [layoutManager truncatedGlyphRangeInLineFragmentForGlyphAtIndex:range.location];
295+
BOOL viewIsTruncated = NSIntersectionRange(range, truncatedGlyphRange).length != 0;
293296

294297
RCTLayoutContext localLayoutContext = layoutContext;
295298
localLayoutContext.absolutePosition.x += frame.origin.x;
@@ -300,9 +303,11 @@ - (void)layoutSubviewsWithContext:(RCTLayoutContext)layoutContext
300303
layoutDirection:self.layoutMetrics.layoutDirection
301304
layoutContext:localLayoutContext];
302305

303-
// Reinforcing a proper frame origin for the Shadow View.
304306
RCTLayoutMetrics localLayoutMetrics = shadowView.layoutMetrics;
305-
localLayoutMetrics.frame.origin = frame.origin;
307+
localLayoutMetrics.frame.origin = frame.origin; // Reinforcing a proper frame origin for the Shadow View.
308+
if (viewIsTruncated) {
309+
localLayoutMetrics.displayType = RCTDisplayTypeNone;
310+
}
306311
[shadowView layoutWithMetrics:localLayoutMetrics layoutContext:localLayoutContext];
307312
}
308313
];

React/Modules/RCTUIManager.m

+7
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ - (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView *
488488
UIUserInterfaceLayoutDirection layoutDirection;
489489
BOOL isNew;
490490
BOOL parentIsNew;
491+
RCTDisplayType displayType;
491492
} RCTFrameData;
492493

493494
// Construct arrays then hand off to main thread
@@ -505,6 +506,7 @@ - (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView *
505506
layoutMetrics.layoutDirection,
506507
shadowView.isNewView,
507508
shadowView.superview.isNewView,
509+
layoutMetrics.displayType
508510
};
509511
}
510512
}
@@ -566,6 +568,7 @@ - (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView *
566568
RCTLayoutAnimation *updatingLayoutAnimation = isNew ? nil : layoutAnimationGroup.updatingLayoutAnimation;
567569
BOOL shouldAnimateCreation = isNew && !frameData.parentIsNew;
568570
RCTLayoutAnimation *creatingLayoutAnimation = shouldAnimateCreation ? layoutAnimationGroup.creatingLayoutAnimation : nil;
571+
BOOL isHidden = frameData.displayType == RCTDisplayTypeNone;
569572

570573
void (^completion)(BOOL) = ^(BOOL finished) {
571574
completionsCalled++;
@@ -581,6 +584,10 @@ - (RCTViewManagerUIBlock)uiBlockWithLayoutUpdateForRootView:(RCTRootShadowView *
581584
if (view.reactLayoutDirection != layoutDirection) {
582585
view.reactLayoutDirection = layoutDirection;
583586
}
587+
588+
if (view.isHidden != isHidden) {
589+
view.hidden = isHidden;
590+
}
584591

585592
if (creatingLayoutAnimation) {
586593

0 commit comments

Comments
 (0)