diff --git a/WebExample/__tests__/styles.spec.ts b/WebExample/__tests__/styles.spec.ts index 562cf1fcd..fe54e9044 100644 --- a/WebExample/__tests__/styles.spec.ts +++ b/WebExample/__tests__/styles.spec.ts @@ -28,7 +28,7 @@ test.describe('markdown content styling', () => { }); test('h1', async ({page}) => { - await testMarkdownContentStyle({testContent: 'header1', style: 'font-size: 25px; font-weight: bold;', page}); + await testMarkdownContentStyle({testContent: 'header1', style: 'font-size: 25px; line-height: 50px; font-weight: bold;', page}); }); test('inline code', async ({page}) => { diff --git a/android/src/main/java/com/expensify/livemarkdown/MarkdownFormatter.java b/android/src/main/java/com/expensify/livemarkdown/MarkdownFormatter.java index 2ef8ccf97..1278d84c5 100644 --- a/android/src/main/java/com/expensify/livemarkdown/MarkdownFormatter.java +++ b/android/src/main/java/com/expensify/livemarkdown/MarkdownFormatter.java @@ -106,10 +106,10 @@ private void applyRange(@NonNull SpannableStringBuilder ssb, @NonNull MarkdownRa break; case "h1": setSpan(ssb, new MarkdownBoldSpan(), start, end); - CustomLineHeightSpan[] spans = ssb.getSpans(0, ssb.length(), CustomLineHeightSpan.class); - if (spans.length >= 1) { - int lineHeight = spans[0].getLineHeight(); - setSpan(ssb, new MarkdownLineHeightSpan(lineHeight * 1.5f), start, end); + float lineHeight = markdownStyle.getH1LineHeight(); + if (lineHeight != -1) { + // NOTE: actually, we should also include "# " but it also works this way + setSpan(ssb, new MarkdownLineHeightSpan(lineHeight), start, end); } // NOTE: size span must be set after line height span to avoid height jumps setSpan(ssb, new MarkdownFontSizeSpan(markdownStyle.getH1FontSize()), start, end); diff --git a/android/src/main/java/com/expensify/livemarkdown/MarkdownStyle.java b/android/src/main/java/com/expensify/livemarkdown/MarkdownStyle.java index 50606f982..92f24cc41 100644 --- a/android/src/main/java/com/expensify/livemarkdown/MarkdownStyle.java +++ b/android/src/main/java/com/expensify/livemarkdown/MarkdownStyle.java @@ -21,6 +21,8 @@ public class MarkdownStyle { private final float mH1FontSize; + private final float mH1LineHeight; + private final float mEmojiFontSize; @ColorInt @@ -76,6 +78,7 @@ public MarkdownStyle(@NonNull ReadableMap map, @NonNull Context context) { mSyntaxColor = parseColor(map, "syntax", "color", context); mLinkColor = parseColor(map, "link", "color", context); mH1FontSize = parseFloat(map, "h1", "fontSize"); + mH1LineHeight = parseFloat(map, "h1", "lineHeight"); mEmojiFontSize = parseFloat(map, "emoji", "fontSize"); mBlockquoteBorderColor = parseColor(map, "blockquote", "borderColor", context); mBlockquoteBorderWidth = parseFloat(map, "blockquote", "borderWidth"); @@ -138,6 +141,10 @@ public float getH1FontSize() { return mH1FontSize; } + public float getH1LineHeight() { + return mH1LineHeight; + } + public float getEmojiFontSize() { return mEmojiFontSize; } diff --git a/android/src/main/java/com/expensify/livemarkdown/spans/MarkdownLineHeightSpan.java b/android/src/main/java/com/expensify/livemarkdown/spans/MarkdownLineHeightSpan.java index 3f4c33fc5..936020e70 100644 --- a/android/src/main/java/com/expensify/livemarkdown/spans/MarkdownLineHeightSpan.java +++ b/android/src/main/java/com/expensify/livemarkdown/spans/MarkdownLineHeightSpan.java @@ -3,16 +3,19 @@ import android.graphics.Paint; import android.text.style.LineHeightSpan; +import com.facebook.react.uimanager.PixelUtil; +import com.facebook.react.views.text.internal.span.CustomLineHeightSpan; + public class MarkdownLineHeightSpan implements MarkdownSpan, LineHeightSpan { - private final float mLineHeight; + private final CustomLineHeightSpan mCustomLineHeightSpan; public MarkdownLineHeightSpan(float lineHeight) { - mLineHeight = lineHeight; + mCustomLineHeightSpan = new CustomLineHeightSpan(PixelUtil.toPixelFromDIP(lineHeight)); } @Override public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int lineHeight, Paint.FontMetricsInt fm) { - fm.top -= mLineHeight / 4; - fm.ascent -= mLineHeight / 4; + // CustomLineHeightSpan is marked as final, we can't extend it, but we can use it via this adapter + mCustomLineHeightSpan.chooseHeight(text, start, end, spanstartv, lineHeight, fm); } } diff --git a/apple/MarkdownFormatter.mm b/apple/MarkdownFormatter.mm index fdb4116f6..069175891 100644 --- a/apple/MarkdownFormatter.mm +++ b/apple/MarkdownFormatter.mm @@ -112,6 +112,12 @@ - (void)applyRangeToAttributedString:(NSMutableAttributedString *)attributedStri paragraphStyle.headIndent = indent; [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range]; [attributedString addAttribute:RCTLiveMarkdownBlockquoteDepthAttributeName value:@(depth) range:range]; + } else if (type == "h1" && markdownStyle.h1LineHeight != -1) { + NSParagraphStyle *defaultParagraphStyle = defaultTextAttributes[NSParagraphStyleAttributeName]; + NSMutableParagraphStyle *paragraphStyle = defaultParagraphStyle != nil ? [defaultParagraphStyle mutableCopy] : [NSMutableParagraphStyle new]; + paragraphStyle.lineHeightMultiple = markdownStyle.h1LineHeight / markdownStyle.h1FontSize; + NSRange rangeWithHashAndSpace = NSMakeRange(range.location - 2, range.length + 2); + [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:rangeWithHashAndSpace]; } else if (type == "pre") { [attributedString addAttribute:NSForegroundColorAttributeName value:markdownStyle.preColor range:range]; NSRange rangeForBackground = [[attributedString string] characterAtIndex:range.location] == '\n' ? NSMakeRange(range.location + 1, range.length - 1) : range; diff --git a/apple/RCTMarkdownStyle.h b/apple/RCTMarkdownStyle.h index 75a27f168..0c1c1edc9 100644 --- a/apple/RCTMarkdownStyle.h +++ b/apple/RCTMarkdownStyle.h @@ -11,6 +11,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic) UIColor *syntaxColor; @property (nonatomic) UIColor *linkColor; @property (nonatomic) CGFloat h1FontSize; +@property (nonatomic) CGFloat h1LineHeight; @property (nonatomic) CGFloat emojiFontSize; @property (nonatomic) UIColor *blockquoteBorderColor; @property (nonatomic) CGFloat blockquoteBorderWidth; diff --git a/apple/RCTMarkdownStyle.mm b/apple/RCTMarkdownStyle.mm index a2752cdff..1a031a0ee 100644 --- a/apple/RCTMarkdownStyle.mm +++ b/apple/RCTMarkdownStyle.mm @@ -18,6 +18,7 @@ - (instancetype)initWithStruct:(const facebook::react::MarkdownTextInputDecorato _linkColor = RCTUIColorFromSharedColor(style.link.color); _h1FontSize = style.h1.fontSize; + _h1LineHeight = style.h1.lineHeight; _emojiFontSize = style.emoji.fontSize; @@ -59,6 +60,7 @@ - (instancetype)initWithDictionary:(NSDictionary *)json _linkColor = [RCTConvert UIColor:json[@"link"][@"color"]]; _h1FontSize = [RCTConvert CGFloat:json[@"h1"][@"fontSize"]]; + _h1LineHeight = [RCTConvert CGFloat:json[@"h1"][@"lineHeight"]]; _emojiFontSize = [RCTConvert CGFloat:json[@"emoji"][@"fontSize"]]; diff --git a/example/src/App.tsx b/example/src/App.tsx index 2d48a2bd6..370ebc200 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -49,6 +49,9 @@ export default function App() { link: { color: linkColorState ? 'red' : 'blue', }, + h1: { + lineHeight: 50, + }, }; }, [emojiFontSizeState, linkColorState]); diff --git a/src/MarkdownTextInputDecoratorViewNativeComponent.ts b/src/MarkdownTextInputDecoratorViewNativeComponent.ts index 2ad81e44c..34e234338 100644 --- a/src/MarkdownTextInputDecoratorViewNativeComponent.ts +++ b/src/MarkdownTextInputDecoratorViewNativeComponent.ts @@ -15,6 +15,7 @@ interface MarkdownStyle { }; h1: { fontSize: Float; + lineHeight: Float; }; blockquote: { borderColor: ColorValue; diff --git a/src/styleUtils.ts b/src/styleUtils.ts index 47cfa5295..bb2e63439 100644 --- a/src/styleUtils.ts +++ b/src/styleUtils.ts @@ -20,6 +20,7 @@ function makeDefaultMarkdownStyle(): MarkdownStyle { }, h1: { fontSize: 25, + lineHeight: -1, }, emoji: { fontSize: 20,