8000 fix(core): Proper line-height calculation by CatchABus · Pull Request #10642 · NativeScript/NativeScript · GitHub
[go: up one dir, main page]

Skip to content

fix(core): Proper line-height calculation #10642

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

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 4 additions & 2 deletions packages/core/platforms/ios/src/UIView+NativeScript.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ - (void)nativeScriptSetTextDecorationAndTransform:(NSString*)text textDecoration
BOOL isTextView = [self isKindOfClass:[UITextView class]];
if (lineHeight > 0) {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = lineHeight;
// Note: Avoid using lineSpacing as it will append the height as extra space
paragraphStyle.minimumLineHeight = lineHeight;
// make sure a possible previously set text alignment setting is not lost when line height is specified
if ([self isKindOfClass:[UIButton class]]) {
paragraphStyle.alignment = ((UIButton*)self).titleLabel.textAlignment;
Expand Down Expand Up @@ -88,7 +89,8 @@ -(void)nativeScriptSetFormattedTextDecorationAndTransform:(NSDictionary*)details
BOOL isLabel = [self isKindOfClass:[UILabel class]];
if (lineHeight > 0) {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = lineHeight;
// Note: Avoid using lineSpacing as it will append the height as extra space
paragraphStyle.minimumLineHeight = lineHeight;
// make sure a possible previously set text alignment setting is not lost when line height is specified
if ([self isKindOfClass:[UIButton class]]) {
paragraphStyle.alignment = ((UIButton*)self).titleLabel.textAlignment;
Expand Down
1 change: 0 additions & 1 deletion packages/core/ui/styling/style-properties.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export const minWidthProperty: CssProperty<Style, CoreTypes.dip | CoreTypes.Leng
export const minHeightProperty: CssProperty<Style, CoreTypes.dip | CoreTypes.LengthDipUnit | CoreTypes.LengthPxUnit>;
export const widthProperty: CssAnimationProperty<Style, CoreTypes.PercentLengthType>;
export const heightProperty: CssAnimationProperty<Style, CoreTypes.PercentLengthType>;
export const lineHeightProperty: CssProperty<Style, number DBD3 >;
export const marginProperty: ShorthandProperty<Style, string | CoreTypes.PercentLengthType>;
export const marginLeftProperty: CssProperty<Style, CoreTypes.PercentLengthType>;
export const marginRightProperty: CssProperty<Style, CoreTypes.PercentLengthType>;
Expand Down
14 changes: 12 additions & 2 deletions packages/core/ui/text-base/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,20 @@ export class TextBase extends TextBaseCommon {
}

[lineHeightProperty.getDefault](): number {
return this.nativeTextViewProtected.getLineSpacingExtra() / layout.getDisplayDensity();
return this.nativeTextViewProtected.getLineHeight() / layout.getDisplayDensity();
}
[lineHeightProperty.setNative](value: number) {
this.nativeTextViewProtected.setLineSpacing(value * layout.getDisplayDensity(), 1);
const dpValue = value * layout.getDisplayDensity();

if (SDK_VERSION >= 28) {
this.nativeTextViewProtected.setLineHeight(dpValue);
} else {
const fontHeight = this.nativeTextViewProtected.getPaint().getFontMetricsInt(null);
// Actual line spacing is the diff of line height and font height
const lineSpacing = Math.max(dpValue - fontHeight, 0);

this.nativeTextViewProtected.setLineSpacing(lineSpacing, 1);
}
}

[fontInternalProperty.getDefault](): android.graphics.Typeface {
Expand Down
Loading
0