99#import " RCTParagraphComponentAccessibilityProvider.h"
1010
1111#import < MobileCoreServices/UTCoreTypes.h>
12+ #import < react/featureflags/ReactNativeFeatureFlags.h>
1213#import < react/renderer/components/text/ParagraphComponentDescriptor.h>
1314#import < react/renderer/components/text/ParagraphProps.h>
1415#import < react/renderer/components/text/ParagraphState.h>
2425
2526using namespace facebook ::react;
2627
28+ @interface RCTTextLayoutManager (RCTParagraphComponentViewPrivate)
29+
30+ - (CGRect)drawingFrameForAttributedString : (facebook::react::AttributedString)attributedString
31+ paragraphAttributes : (facebook::react::ParagraphAttributes)paragraphAttributes
32+ frame : (CGRect)frame
33+ containerFrame : (CGRect *)containerFrame ;
34+
35+ @end
36+
2737// ParagraphTextView is an auxiliary view we set as contentView so the drawing
2838// can happen on top of the layers manipulated by RCTViewComponentView (the parent view)
2939@interface RCTParagraphTextView : UIView
3040
3141@property (nonatomic ) ParagraphShadowNode::ConcreteState::Shared state;
3242@property (nonatomic ) ParagraphAttributes paragraphAttributes;
3343@property (nonatomic ) LayoutMetrics layoutMetrics;
44+ @property (nonatomic ) CGRect drawingFrame;
3445
3546@end
3647
@@ -50,6 +61,7 @@ @implementation RCTParagraphComponentView {
5061 RCTParagraphComponentAccessibilityProvider *_accessibilityProvider;
5162 UILongPressGestureRecognizer *_longPressGestureRecognizer;
5263 RCTParagraphTextView *_textView;
64+ CGRect _textLayoutFrame;
5365}
5466
5567- (instancetype )initWithFrame : (CGRect)frame
@@ -60,6 +72,7 @@ - (instancetype)initWithFrame:(CGRect)frame
6072 self.opaque = NO ;
6173 _textView = [RCTParagraphTextView new ];
6274 _textView.backgroundColor = UIColor.clearColor ;
75+ _textView.drawingFrame = self.bounds ;
6376 self.contentView = _textView;
6477 }
6578
@@ -134,6 +147,7 @@ - (void)updateLayoutMetrics:(const LayoutMetrics &)layoutMetrics
134147 // re-applying individual sub-values which weren't changed.
135148 [super updateLayoutMetrics: layoutMetrics oldLayoutMetrics: _layoutMetrics];
136149 _textView.layoutMetrics = _layoutMetrics;
150+ _textLayoutFrame = RCTCGRectFromRect (_layoutMetrics.getContentFrame ());
137151 [_textView setNeedsDisplay ];
138152 [self setNeedsLayout ];
139153}
@@ -149,7 +163,28 @@ - (void)layoutSubviews
149163{
150164 [super layoutSubviews ];
151165
152- _textView.frame = self.bounds ;
166+ CGRect textViewFrame = self.bounds ;
167+ CGRect drawingFrame = RCTCGRectFromRect (_layoutMetrics.getContentFrame ());
168+
169+ if (ReactNativeFeatureFlags::enableIOSCompressedTextFrameAdjustment () && _textView.state &&
170+ drawingFrame.size .height > 0 ) {
171+ const auto &stateData = _textView.state ->getData ();
172+ auto textLayoutManager = stateData.layoutManager .lock ();
173+ if (textLayoutManager) {
174+ RCTTextLayoutManager *nativeTextLayoutManager =
175+ (RCTTextLayoutManager *)unwrapManagedObject (textLayoutManager->getNativeTextLayoutManager ());
176+ CGRect drawingContainerFrame = drawingFrame;
177+ drawingFrame = [nativeTextLayoutManager drawingFrameForAttributedString: stateData.attributedString
178+ paragraphAttributes: _paragraphAttributes
179+ frame: drawingFrame
180+ containerFrame: &drawingContainerFrame];
181+ textViewFrame = CGRectUnion (textViewFrame, drawingContainerFrame);
182+ }
183+ }
184+
185+ _textLayoutFrame = drawingFrame;
186+ _textView.frame = textViewFrame;
187+ _textView.drawingFrame = CGRectOffset (drawingFrame, -textViewFrame.origin .x , -textViewFrame.origin .y );
153188}
154189
155190#pragma mark - Accessibility
@@ -194,7 +229,7 @@ - (NSArray *)accessibilityElements
194229 if (textLayoutManager) {
195230 RCTTextLayoutManager *nativeTextLayoutManager =
196231 (RCTTextLayoutManager *)unwrapManagedObject (textLayoutManager->getNativeTextLayoutManager ());
197- CGRect frame = RCTCGRectFromRect (_layoutMetrics. getContentFrame ()) ;
232+ CGRect frame = _textLayoutFrame ;
198233 _accessibilityProvider =
199234 [[RCTParagraphComponentAccessibilityProvider alloc ] initWithString: data.attributedString
200235 layoutManager: nativeTextLayoutManager
@@ -268,7 +303,7 @@ - (SharedTouchEventEmitter)touchEventEmitterAtPoint:(CGPoint)point
268303
269304 RCTTextLayoutManager *nativeTextLayoutManager =
270305 (RCTTextLayoutManager *)unwrapManagedObject (textLayoutManager->getNativeTextLayoutManager ());
271- CGRect frame = RCTCGRectFromRect (_layoutMetrics. getContentFrame ()) ;
306+ CGRect frame = _textLayoutFrame ;
272307
273308 auto eventEmitter = [nativeTextLayoutManager getEventEmitterWithAttributeString: stateData.attributedString
274309 paragraphAttributes: _paragraphAttributes
@@ -404,7 +439,7 @@ - (void)drawRect:(CGRect)rect
404439 RCTTextLayoutManager *nativeTextLayoutManager =
405440 (RCTTextLayoutManager *)unwrapManagedObject (textLayoutManager->getNativeTextLayoutManager ());
406441
407- CGRect frame = RCTCGRectFromRect (_layoutMetrics. getContentFrame ()) ;
442+ CGRect frame = _drawingFrame ;
408443
409444 [nativeTextLayoutManager drawAttributedString: stateData.attributedString
410445 paragraphAttributes: _paragraphAttributes
0 commit comments