Skip to content

Commit ec7fa5d

Browse files
authored
fix(core): proper line-height calculation (#10642)
1 parent 4f46815 commit ec7fa5d

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

packages/core/platforms/ios/src/UIView+NativeScript.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ - (void)nativeScriptSetTextDecorationAndTransform:(NSString*)text textDecoration
2626
BOOL isTextView = [self isKindOfClass:[UITextView class]];
2727
if (lineHeight > 0) {
2828
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
29-
paragraphStyle.lineSpacing = lineHeight;
29+
// Note: Avoid using lineSpacing as it will append the height as extra space
30+
paragraphStyle.minimumLineHeight = lineHeight;
3031
// make sure a possible previously set text alignment setting is not lost when line height is specified
3132
if ([self isKindOfClass:[UIButton class]]) {
3233
paragraphStyle.alignment = ((UIButton*)self).titleLabel.textAlignment;
@@ -88,7 +89,8 @@ -(void)nativeScriptSetFormattedTextDecorationAndTransform:(NSDictionary*)details
8889
BOOL isLabel = [self isKindOfClass:[UILabel class]];
8990
if (lineHeight > 0) {
9091
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
91-
paragraphStyle.lineSpacing = lineHeight;
92+
// Note: Avoid using lineSpacing as it will append the height as extra space
93+
paragraphStyle.minimumLineHeight = lineHeight;
9294
// make sure a possible previously set text alignment setting is not lost when line height is specified
9395
if ([self isKindOfClass:[UIButton class]]) {
9496
paragraphStyle.alignment = ((UIButton*)self).titleLabel.textAlignment;

packages/core/ui/styling/style-properties.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ export const minWidthProperty: CssProperty<Style, CoreTypes.dip | CoreTypes.Leng
7777
export const minHeightProperty: CssProperty<Style, CoreTypes.dip | CoreTypes.LengthDipUnit | CoreTypes.LengthPxUnit>;
7878
export const widthProperty: CssAnimationProperty<Style, CoreTypes.PercentLengthType>;
7979
export const heightProperty: CssAnimationProperty<Style, CoreTypes.PercentLengthType>;
80-
export const lineHeightProperty: CssProperty<Style, number>;
8180
export const marginProperty: ShorthandProperty<Style, string | CoreTypes.PercentLengthType>;
8281
export const marginLeftProperty: CssProperty<Style, CoreTypes.PercentLengthType>;
8382
export const marginRightProperty: CssProperty<Style, CoreTypes.PercentLengthType>;

packages/core/ui/text-base/index.android.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,20 @@ export class TextBase extends TextBaseCommon {
374374
}
375375

376376
[lineHeightProperty.getDefault](): number {
377-
return this.nativeTextViewProtected.getLineSpacingExtra() / layout.getDisplayDensity();
377+
return this.nativeTextViewProtected.getLineHeight() / layout.getDisplayDensity();
378378
}
379379
[lineHeightProperty.setNative](value: number) {
380-
this.nativeTextViewProtected.setLineSpacing(value * layout.getDisplayDensity(), 1);
380+
const dpValue = value * layout.getDisplayDensity();
381+
382+
if (SDK_VERSION >= 28) {
383+
this.nativeTextViewProtected.setLineHeight(dpValue);
384+
} else {
385+
const fontHeight = this.nativeTextViewProtected.getPaint().getFontMetricsInt(null);
386+
// Actual line spacing is the diff of line height and font height
387+
const lineSpacing = Math.max(dpValue - fontHeight, 0);
388+
389+
this.nativeTextViewProtected.setLineSpacing(lineSpacing, 1);
390+
}
381391
}
382392

383393
[fontInternalProperty.getDefault](): android.graphics.Typeface {

0 commit comments

Comments
 (0)