Skip to content

Commit 01f7ab8

Browse files
j-piaseckifacebook-github-bot
authored andcommitted
Fix text baseline being moved up on iOS (facebook#44932)
Summary: Fixes facebook#44929 Moves calling `RCTApplyBaselineOffset` so it's called in similar way as on the old architecture: https://github.com/facebook/react-native/blob/726a4a1e5ef3bc8034a068145da2bb94ae3acfaa/packages/react-native/Libraries/Text/Text/RCTTextShadowView.mm#L229 https://github.com/facebook/react-native/blob/726a4a1e5ef3bc8034a068145da2bb94ae3acfaa/packages/react-native/Libraries/Text/Text/RCTTextShadowView.mm#L165-L167 I'm not exactly sure why (and Apple's docs aren't helping) but it looks like calling `NSTextStorage initWithAttributedString` results in an attributed string where emojis are separate fragments with Apple's emoji font set, which results in the correct baseline calculations. This is the way it happens on the old arch. The way it's currently implemented on the new architecture, `RCTApplyBaselineOffset` is called on ` newly created attributed string where emojis are in the same fragment as other parts of the text which alters the result of baseline calculations compared to what's later drawn on the screen. ## Changelog: [IOS] [FIXED] - Fixed text baseline being moved upwards in certain cases Pull Request resolved: facebook#44932 Test Plan: <details> <summary>Check the following snipped</summary> ```jsx import React from 'react'; import { Text, View, } from 'react-native'; const App = () => { return ( <View style={{ paddingTop: 100, }}> <Text style={{fontSize: 15, lineHeight: 20, fontFamily: 'Arial', backgroundColor: 'gray'}}>A 😞😞😞 B</Text> </View> ); }; export default App; ``` </details> |Before|After| |-|-| |<img width="546" alt="new-broken" src="https://github.com/facebook/react-native/assets/21055725/cc0a3841-beb5-4afc-8abf-1124312fa6e0">|<img width="546" alt="new-fixed" src="https://github.com/facebook/react-native/assets/21055725/0ec2f6f1-3c62-4537-9a75-6abb840633f7">| Reviewed By: cipolleschi Differential Revision: D58589249 Pulled By: sammy-SC fbshipit-source-id: 1e0a730519a5c79ff8a35b0dfb16b68966a8dd9f
1 parent 37d7ec6 commit 01f7ab8

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ facebook::react::AttributedStringBox RCTAttributedStringBoxFromNSAttributedStrin
3939

4040
NSString *RCTNSStringFromStringApplyingTextTransform(NSString *string, facebook::react::TextTransform textTransform);
4141

42+
void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText);
43+
4244
@interface RCTWeakEventEmitterWrapper : NSObject
4345
@property (nonatomic, assign) facebook::react::SharedEventEmitter eventEmitter;
4446
@end

packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
300300
return [attributes copy];
301301
}
302302

303-
static void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText)
303+
void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText)
304304
{
305305
__block CGFloat maximumLineHeight = 0;
306306

@@ -411,7 +411,6 @@ static void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText)
411411

412412
[nsAttributedString appendAttributedString:nsAttributedStringFragment];
413413
}
414-
RCTApplyBaselineOffset(nsAttributedString);
415414
[nsAttributedString endEditing];
416415

417416
return nsAttributedString;

packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ - (NSTextStorage *)_textStorageAndLayoutManagerWithAttributesString:(NSAttribute
185185

186186
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];
187187

188+
RCTApplyBaselineOffset(textStorage);
189+
188190
[textStorage addLayoutManager:layoutManager];
189191

190192
if (paragraphAttributes.adjustsFontSizeToFit) {

0 commit comments

Comments
 (0)