Skip to content

Commit 832229f

Browse files
committed
Fabric implementation of react/react-native#38359
1 parent a7fafd7 commit 832229f

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

ios/RCTMarkdownUtils.mm

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ - (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input withA
136136
}
137137
}];
138138

139+
RCTApplyBaselineOffset(attributedString);
140+
139141
[attributedString endEditing];
140142

141143
_prevInputString = inputString;
@@ -148,4 +150,47 @@ - (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input withA
148150
}
149151
}
150152

153+
static void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText)
154+
{
155+
__block CGFloat maximumLineHeight = 0;
156+
157+
[attributedText enumerateAttribute:NSParagraphStyleAttributeName
158+
inRange:NSMakeRange(0, attributedText.length)
159+
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
160+
usingBlock:^(NSParagraphStyle *paragraphStyle, __unused NSRange range, __unused BOOL *stop) {
161+
if (!paragraphStyle) {
162+
return;
163+
}
164+
165+
maximumLineHeight = MAX(paragraphStyle.maximumLineHeight, maximumLineHeight);
166+
}];
167+
168+
if (maximumLineHeight == 0) {
169+
// `lineHeight` was not specified, nothing to do.
170+
return;
171+
}
172+
173+
__block CGFloat maximumFontLineHeight = 0;
174+
175+
[attributedText enumerateAttribute:NSFontAttributeName
176+
inRange:NSMakeRange(0, attributedText.length)
177+
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
178+
usingBlock:^(UIFont *font, NSRange range, __unused BOOL *stop) {
179+
if (!font) {
180+
return;
181+
}
182+
183+
maximumFontLineHeight = MAX(font.lineHeight, maximumFontLineHeight);
184+
}];
185+
186+
if (maximumLineHeight < maximumFontLineHeight) {
187+
return;
188+
}
189+
190+
CGFloat baseLineOffset = (maximumLineHeight - maximumFontLineHeight) / 2.0;
191+
[attributedText addAttribute:NSBaselineOffsetAttributeName
192+
value:@(baseLineOffset)
193+
range:NSMakeRange(0, attributedText.length)];
194+
}
195+
151196
@end

0 commit comments

Comments
 (0)