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

This commit is contained in:
Dimitris-Rafail Katsampas
2024-10-31 23:40:23 +02:00
committed by GitHub
parent 4f46815b27
commit ec7fa5d05e
3 changed files with 16 additions and 5 deletions

View File

@ -26,7 +26,8 @@
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;
@ -88,7 +89,8 @@
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;

View File

@ -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>;
export const marginProperty: ShorthandProperty<Style, string | CoreTypes.PercentLengthType>;
export const marginLeftProperty: CssProperty<Style, CoreTypes.PercentLengthType>;
export const marginRightProperty: CssProperty<Style, CoreTypes.PercentLengthType>;

View File

@ -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 {