Skip to content

Commit

Permalink
Apply baseline offset for each paragraph individually
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw committed Dec 21, 2024
1 parent 61a4918 commit fc1d1e9
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions apple/MarkdownFormatter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ - (nonnull NSAttributedString *)format:(nonnull NSString *)text
defaultTextAttributes:defaultTextAttributes];
}

RCTApplyBaselineOffset(attributedString);
[attributedString.string enumerateSubstringsInRange:NSMakeRange(0, attributedString.length)
options:NSStringEnumerationByLines | NSStringEnumerationSubstringNotRequired
usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
RCTApplyBaselineOffset(attributedString, enclosingRange);
}];

[attributedString endEditing];

Expand Down Expand Up @@ -116,12 +120,12 @@ - (void)applyRangeToAttributedString:(NSMutableAttributedString *)attributedStri
}
}

static void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText)
static void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText, NSRange attributedTextRange)
{
__block CGFloat maximumLineHeight = 0;

[attributedText enumerateAttribute:NSParagraphStyleAttributeName
inRange:NSMakeRange(0, attributedText.length)
inRange:attributedTextRange
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
usingBlock:^(NSParagraphStyle *paragraphStyle, __unused NSRange range, __unused BOOL *stop) {
if (!paragraphStyle) {
Expand All @@ -139,7 +143,7 @@ static void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText)
__block CGFloat maximumFontLineHeight = 0;

[attributedText enumerateAttribute:NSFontAttributeName
inRange:NSMakeRange(0, attributedText.length)
inRange:attributedTextRange
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
usingBlock:^(UIFont *font, NSRange range, __unused BOOL *stop) {
if (!font) {
Expand All @@ -156,7 +160,7 @@ static void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText)
CGFloat baseLineOffset = (maximumLineHeight - maximumFontLineHeight) / 2.0;
[attributedText addAttribute:NSBaselineOffsetAttributeName
value:@(baseLineOffset)
range:NSMakeRange(0, attributedText.length)];
range:attributedTextRange];
}

@end

0 comments on commit fc1d1e9

Please sign in to comment.