@@ -305,6 +305,50 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
305
305
return [attributes copy ];
306
306
}
307
307
308
+ static void RCTApplyBaselineOffset (NSMutableAttributedString *attributedText)
309
+ {
310
+ __block CGFloat maximumLineHeight = 0 ;
311
+
312
+ [attributedText enumerateAttribute: NSParagraphStyleAttributeName
313
+ inRange: NSMakeRange (0 , attributedText.length)
314
+ options: NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
315
+ usingBlock: ^(NSParagraphStyle *paragraphStyle, __unused NSRange range, __unused BOOL *stop) {
316
+ if (!paragraphStyle) {
317
+ return ;
318
+ }
319
+
320
+ maximumLineHeight = MAX (paragraphStyle.maximumLineHeight , maximumLineHeight);
321
+ }];
322
+
323
+ if (maximumLineHeight == 0 ) {
324
+ // `lineHeight` was not specified, nothing to do.
325
+ return ;
326
+ }
327
+
328
+ __block CGFloat maximumFontLineHeight = 0 ;
329
+
330
+ [attributedText enumerateAttribute: NSFontAttributeName
331
+ inRange: NSMakeRange (0 , attributedText.length)
332
+ options: NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
333
+ usingBlock: ^(UIFont *font, NSRange range, __unused BOOL *stop) {
334
+ if (!font) {
335
+ return ;
336
+ }
337
+
338
+ maximumFontLineHeight = MAX (font.lineHeight , maximumFontLineHeight);
339
+ }];
340
+
341
+ if (maximumLineHeight < maximumFontLineHeight) {
342
+ return ;
343
+ }
344
+
345
+ CGFloat baseLineOffset = (maximumLineHeight - maximumFontLineHeight) / 2.0 ;
346
+
347
+ [attributedText addAttribute: NSBaselineOffsetAttributeName
348
+ value: @(baseLineOffset)
349
+ range: NSMakeRange (0 , attributedText.length)];
350
+ }
351
+
308
352
NSAttributedString *RCTNSAttributedStringFromAttributedString (const AttributedString &attributedString)
309
353
{
310
354
static UIImage *placeholderImage;
@@ -357,7 +401,7 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
357
401
358
402
[nsAttributedString appendAttributedString: nsAttributedStringFragment];
359
403
}
360
-
404
+ RCTApplyBaselineOffset (nsAttributedString);
361
405
[nsAttributedString endEditing ];
362
406
363
407
return nsAttributedString;
0 commit comments