Skip to content

Customize line height for headings #535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 30 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4c16500
Customize line height for headings
tomekzaw Nov 5, 2024
29b8bc9
Fix web tests
tomekzaw Nov 5, 2024
3ccb597
Merge branch 'main' into @tomekzaw/line-height
tomekzaw Nov 6, 2024
30ab8bd
Update Podfile.lock
tomekzaw Nov 6, 2024
5ba3332
Merge branch 'main' into @tomekzaw/line-height
tomekzaw Dec 7, 2024
579a2aa
Merge branch 'main' into @tomekzaw/line-height
tomekzaw Dec 9, 2024
0c0a6dc
Update Podfile.lock
tomekzaw Dec 9, 2024
abf5e6b
Merge branch 'main' into @tomekzaw/line-height
tomekzaw Dec 22, 2024
a08da3b
Update Podfile.lock
tomekzaw Dec 22, 2024
3ced129
Unify `NSMutableParagraphStyle` creation
tomekzaw Dec 22, 2024
6a1a99b
Merge branch 'main' into @tomekzaw/line-height
tomekzaw Jan 10, 2025
4b32ede
Merge branch 'main' into @tomekzaw/line-height
tomekzaw Jan 31, 2025
06b1ef4
Merge branch 'main' into @tomekzaw/line-height
tomekzaw Mar 13, 2025
f0451e0
Fix line height for h1 on iOS when line height is set on text input
tomekzaw Mar 14, 2025
6f9838d
Add patch for react-native
tomekzaw Mar 14, 2025
16862ac
Fix heading line height in blockquote
tomekzaw Mar 14, 2025
7ceeadc
Fix line height of regular lines
tomekzaw Mar 14, 2025
027dbda
Merge branch 'main' into @tomekzaw/line-height
tomekzaw Mar 18, 2025
bae14a5
Fix cursor height in empty text input with line height set
tomekzaw Mar 18, 2025
83a5feb
Merge branch 'main' into @tomekzaw/line-height
tomekzaw Mar 18, 2025
65c1d45
Merge branch 'main' into @tomekzaw/line-height
tomekzaw Mar 19, 2025
1850eb8
WIP
tomekzaw Mar 19, 2025
9495a26
Merge branch 'main' into @tomekzaw/line-height
tomekzaw Mar 23, 2025
6d91254
Merge branch 'main' into @tomekzaw/line-height
tomekzaw Mar 25, 2025
11e6265
Merge branch 'main' into @tomekzaw/line-height
tomekzaw Apr 14, 2025
914b5b9
Fix react-native patch
tomekzaw Apr 14, 2025
d47d3f7
Merge branch 'main' into @tomekzaw/line-height
tomekzaw Apr 24, 2025
f262fbe
Merge branch 'main' into @tomekzaw/line-height
tomekzaw May 14, 2025
121b311
Bump react-native patch version
tomekzaw May 15, 2025
11ef181
Add buttons for toggling text and heading line height
tomekzaw May 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion WebExample/__tests__/styles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class MarkdownStyle {

private final float mH1FontSize;

private final float mH1LineHeight;

private final float mEmojiFontSize;

@ColorInt
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -138,6 +141,10 @@ public float getH1FontSize() {
return mH1FontSize;
}

public float getH1LineHeight() {
return mH1LineHeight;
}

public float getEmojiFontSize() {
return mEmojiFontSize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
22 changes: 22 additions & 0 deletions apple/MarkdownFormatter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ - (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) {
__block BOOL found = NO;
[attributedString enumerateAttribute:NSParagraphStyleAttributeName
inRange:range
options:0
usingBlock:^(NSParagraphStyle *paragraphStyle, NSRange paragraphRange, BOOL *stop) {
if (paragraphStyle && paragraphStyle.headIndent && [paragraphStyle isKindOfClass:[NSMutableParagraphStyle class]]) {
NSMutableParagraphStyle *mutableParagraphStyle = (NSMutableParagraphStyle *)paragraphStyle;
mutableParagraphStyle.minimumLineHeight = markdownStyle.h1LineHeight;
mutableParagraphStyle.maximumLineHeight = markdownStyle.h1LineHeight;
found = YES;
*stop = YES;
}
}];
if (!found) {
NSParagraphStyle *defaultParagraphStyle = defaultTextAttributes[NSParagraphStyleAttributeName];
NSMutableParagraphStyle *paragraphStyle = defaultParagraphStyle != nil ? [defaultParagraphStyle mutableCopy] : [NSMutableParagraphStyle new];
paragraphStyle.minimumLineHeight = markdownStyle.h1LineHeight;
paragraphStyle.maximumLineHeight = markdownStyle.h1LineHeight;
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;
Expand Down
1 change: 1 addition & 0 deletions apple/RCTMarkdownStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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;
Expand Down
1 change: 1 addition & 0 deletions apple/RCTMarkdownStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ - (instancetype)initWithStruct:(const facebook::react::MarkdownTextInputDecorato
_linkColor = RCTUIColorFromSharedColor(style.link.color);

_h1FontSize = style.h1.fontSize;
_h1LineHeight = style.h1.lineHeight;

_emojiFontSize = style.emoji.fontSize;

Expand Down
3 changes: 3 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export default function App() {
link: {
color: linkColorState ? 'red' : 'blue',
},
h1: {
lineHeight: 50,
},
};
}, [emojiFontSizeState, linkColorState]);

Expand Down
57 changes: 57 additions & 0 deletions patches/react-native+0.77.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
diff --git a/node_modules/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.h b/node_modules/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.h
index 3427663..903344a 100644
--- a/node_modules/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.h
+++ b/node_modules/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.h
@@ -41,6 +41,8 @@ NSString *RCTNSStringFromStringApplyingTextTransform(NSString *string, facebook:

void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText);

+void RCTApplyBaselineOffsetForRange(NSMutableAttributedString *attributedText, NSRange attributedTextRange);
+
/*
* Whether two `NSAttributedString` lead to the same underlying displayed text, even if they are not strictly equal.
* I.e. is one string substitutable for the other when backing a control (which may have some ignorable attributes
diff --git a/node_modules/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm b/node_modules/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm
index 1e551e4..04c9d3d 100644
--- a/node_modules/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm
+++ b/node_modules/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm
@@ -325,11 +325,20 @@ NSMutableDictionary<NSAttributedStringKey, id> *RCTNSTextAttributesFromTextAttri
}

void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText)
+{
+ [attributedText.string enumerateSubstringsInRange:NSMakeRange(0, attributedText.length)
+ options:NSStringEnumerationByLines | NSStringEnumerationSubstringNotRequired
+ usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
+ RCTApplyBaselineOffsetForRange(attributedText, enclosingRange);
+ }];
+}
+
+void RCTApplyBaselineOffsetForRange(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) {
@@ -347,7 +356,7 @@ 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) {
@@ -365,7 +374,7 @@ void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText)

[attributedText addAttribute:NSBaselineOffsetAttributeName
value:@(baseLineOffset)
- range:NSMakeRange(0, attributedText.length)];
+ range:attributedTextRange];
}

static NSMutableAttributedString *RCTNSAttributedStringFragmentFromFragment(
1 change: 1 addition & 0 deletions src/MarkdownTextInputDecoratorViewNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface MarkdownStyle {
};
h1: {
fontSize: Float;
lineHeight: Float;
};
blockquote: {
borderColor: ColorValue;
Expand Down
1 change: 1 addition & 0 deletions src/styleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function makeDefaultMarkdownStyle(): MarkdownStyle {
},
h1: {
fontSize: 25,
lineHeight: -1,
},
emoji: {
fontSize: 20,
Expand Down
Loading