Skip to content

Commit e2051e4

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 8038ff0 + 9eea570 commit e2051e4

8 files changed

Lines changed: 106 additions & 34 deletions

File tree

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
## [1.6.21](https://github.com/nativescript-community/text/compare/v1.6.20...v1.6.21) (2026-02-09)
7+
8+
### Bug Fixes
9+
10+
* **android:** removed unwanted log ([e94a709](https://github.com/nativescript-community/text/commit/e94a709f1b0d7ab3d3c06a62eb40b4f3cf16bfae))
11+
12+
## [1.6.20](https://github.com/nativescript-community/text/compare/v1.6.19...v1.6.20) (2026-02-08)
13+
14+
### Bug Fixes
15+
16+
* **android:** lineHeight fix ([d945f4b](https://github.com/nativescript-community/text/commit/d945f4b1d3029fb8605d9de1765f698611734b29))
17+
* HeightSPan fix ([92d87c9](https://github.com/nativescript-community/text/commit/92d87c97ab3f9aa244280354e74265da4591d39d))
18+
* **ios:** lineHeight base line offset fix ([bfb9db2](https://github.com/nativescript-community/text/commit/bfb9db2c4e3a24347cf08a4321c97655fc4a031e))
19+
620
## [1.6.19](https://github.com/nativescript-community/text/compare/v1.6.18...v1.6.19) (2026-02-07)
721

822
### Bug Fixes

lerna.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.6.19",
2+
"version": "1.6.21",
33
"$schema": "node_modules/@lerna-lite/cli/schemas/lerna-schema.json",
44
"packages": [
55
"packages/*"

packages/text/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
## [1.6.21](https://github.com/nativescript-community/text/compare/v1.6.20...v1.6.21) (2026-02-09)
7+
8+
### Bug Fixes
9+
10+
* **android:** removed unwanted log ([e94a709](https://github.com/nativescript-community/text/commit/e94a709f1b0d7ab3d3c06a62eb40b4f3cf16bfae))
11+
12+
## [1.6.20](https://github.com/nativescript-community/text/compare/v1.6.19...v1.6.20) (2026-02-08)
13+
14+
### Bug Fixes
15+
16+
* HeightSPan fix ([92d87c9](https://github.com/nativescript-community/text/commit/92d87c97ab3f9aa244280354e74265da4591d39d))
17+
* **ios:** lineHeight base line offset fix ([bfb9db2](https://github.com/nativescript-community/text/commit/bfb9db2c4e3a24347cf08a4321c97655fc4a031e))
18+
619
## [1.6.19](https://github.com/nativescript-community/text/compare/v1.6.18...v1.6.19) (2026-02-07)
720

821
### Bug Fixes

packages/text/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript-community/text",
3-
"version": "1.6.19",
3+
"version": "1.6.21",
44
"description": "Expands the capabilities of NativeScript's text property.",
55
"main": "./index",
66
"sideEffects": false,

packages/text/platforms/android/java/com/nativescript/text/Font.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,7 @@ public static CharSequence fromHtml(Context context, String fontFolder, String p
518518
}
519519
private static float FONT_SIZE_FACTOR = -1;
520520
public static float getFontSizeFactor(Context context) {
521-
if (FONT_SIZE_FACTOR == -1) {
522-
FONT_SIZE_FACTOR = android.util.TypedValue.applyDimension(
521+
return android.util.TypedValue.applyDimension(
523522
android.util.TypedValue.COMPLEX_UNIT_SP, 1, context.getResources().getDisplayMetrics());
524-
}
525-
return FONT_SIZE_FACTOR;
526523
}
527524
}

packages/text/platforms/android/java/com/nativescript/text/HeightSpan.java

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99

1010
import android.graphics.Paint;
1111
import android.text.style.LineHeightSpan;
12+
import android.util.Log;
1213

1314
public class HeightSpan implements LineHeightSpan {
1415
private final int mHeight;
1516

1617
public HeightSpan(float height) {
1718
this.mHeight = (int) Math.ceil(height);
1819
}
19-
20+
public int getHeight() {
21+
return mHeight;
22+
}
2023
@Override
2124
public void chooseHeight(
2225
CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm) {
@@ -25,31 +28,55 @@ public void chooseHeight(
2528
// The general solution is that if there's not enough height to show the full line height, we
2629
// will prioritize in this order: descent, ascent, bottom, top
2730

28-
if (fm.descent > mHeight) {
29-
// Show as much descent as possible
30-
fm.bottom = fm.descent = Math.min(mHeight, fm.descent);
31-
fm.top = fm.ascent = 0;
32-
} else if (-fm.ascent + fm.descent > mHeight) {
33-
// Show all descent, and as much ascent as possible
34-
fm.bottom = fm.descent;
35-
fm.top = fm.ascent = -mHeight + fm.descent;
36-
} else if (-fm.ascent + fm.bottom > mHeight) {
37-
// Show all ascent, descent, as much bottom as possible
38-
fm.top = fm.ascent;
39-
fm.bottom = fm.ascent + mHeight;
40-
} else if (-fm.top + fm.bottom > mHeight) {
41-
// Show all ascent, descent, bottom, as much top as possible
42-
fm.top = fm.bottom - mHeight;
43-
} else {
44-
// Show proportionally additional ascent / top & descent / bottom
45-
final int additional = mHeight - (-fm.top + fm.bottom);
31+
// if (fm.descent > mHeight) {
32+
// // Show as much descent as possible
33+
// fm.bottom = fm.descent = Math.min(mHeight, fm.descent);
34+
// fm.top = fm.ascent = 0;
35+
// } else if (-fm.ascent + fm.descent > mHeight) {
36+
// // Show all descent, and as much ascent as possible
37+
// fm.bottom = fm.descent;
38+
// fm.top = fm.ascent = -mHeight + fm.descent;
39+
// } else if (-fm.ascent + fm.bottom > mHeight) {
40+
// // Show all ascent, descent, as much bottom as possible
41+
// fm.top = fm.ascent;
42+
// fm.bottom = fm.ascent + mHeight;
43+
// } else if (-fm.top + fm.bottom > mHeight) {
44+
// // Show all ascent, descent, bottom, as much top as possible
45+
// fm.top = fm.bottom - mHeight;
46+
// } else {
47+
// // Show proportionally additional ascent / top & descent / bottom
48+
// final int additional = mHeight - (-fm.top + fm.bottom);
4649

47-
// Round up for the negative values and down for the positive values (arbitrary choice)
48-
// So that bottom - top equals additional even if it's an odd number.
49-
fm.top -= Math.ceil(additional / 2.0f);
50-
fm.bottom += Math.floor(additional / 2.0f);
51-
fm.ascent = fm.top;
52-
fm.descent = fm.bottom;
50+
// // Round up for the negative values and down for the positive values (arbitrary choice)
51+
// // So that bottom - top equals additional even if it's an odd number.
52+
// fm.top -= Math.ceil(additional / 2.0f);
53+
// fm.bottom += Math.floor(additional / 2.0f);
54+
// fm.ascent = fm.top;
55+
// fm.descent = fm.bottom;
56+
// }
57+
final int originHeight = fm.descent - fm.ascent;
58+
// If original height is not positive, do nothing.
59+
if (originHeight <= 0) {
60+
return;
61+
}
62+
final int diff = mHeight - originHeight;
63+
if (diff == 0) {
64+
return;
65+
}
66+
67+
if (diff > 0) {
68+
// Expand line: keep glyphs at the top, add extra space below.
69+
fm.bottom += diff;
70+
fm.descent += diff;
71+
} else {
72+
// Shrink line: remove space proportionally from top and bottom (center content).
73+
int reduce = -diff;
74+
int reduceTop = (int) Math.ceil(reduce / 2.0f);
75+
int reduceBottom = reduce - reduceTop;
76+
fm.top += reduceTop;
77+
fm.ascent += reduceTop;
78+
fm.bottom -= reduceBottom;
79+
fm.descent -= reduceBottom;
5380
}
5481
}
55-
}
82+
}

packages/text/platforms/ios/src/NSTextUtils.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class NSTextUtils: NSObject {
9292
class func computeBaseLineOffset(align:String!, fontAscent:Float, fontDescent:Float, fontBottom:Float, fontTop:Float, fontSize:Float, maxFontSize:Float) -> Float {
9393
var result:Float = 0
9494
if (align == "top") {
95-
result = -maxFontSize - fontBottom - fontTop
95+
result = -maxFontSize - fontBottom - fontTop - (fontAscent - fontDescent) / 2
9696
} else if (align == "bottom") {
9797
result = fontBottom
9898
} else if (align == "text-top") {
@@ -249,7 +249,9 @@ class NSTextUtils: NSObject {
249249

250250
}
251251
let verticalTextAlignment:String! = spanDetails.object(forKey: "verticalTextAlignment") as? String
252+
var realVerticalTextAlignment:String! = nil
252253
if (verticalTextAlignment != nil) && (iosFont != nil) && !(verticalTextAlignment == "initial") && !(verticalTextAlignment == "stretch") {
254+
realVerticalTextAlignment = verticalTextAlignment
253255
let fontSize:NSNumber! = fontSize != nil ? fontSize : NSNumber(value: Float(iosFont.pointSize))
254256
let ctFont:CTFont = iosFont
255257
let ascent:CGFloat = CTFontGetAscent(ctFont)
@@ -293,6 +295,21 @@ class NSTextUtils: NSObject {
293295
}
294296
paragraphStyle.maximumLineHeight = fLineHeight
295297
paragraphStyle.minimumLineHeight = fLineHeight
298+
if (iosFont != nil){
299+
let ctFont: CTFont = iosFont
300+
let ascent = CTFontGetAscent(ctFont)
301+
let descent = CTFontGetDescent(ctFont)
302+
let fontSize:NSNumber! = fontSize != nil ? fontSize : NSNumber(value: Float(iosFont.pointSize))
303+
let baseOffset = -computeBaseLineOffset(align: realVerticalTextAlignment != nil ? realVerticalTextAlignment : "top",
304+
fontAscent: -Float(ascent),
305+
fontDescent: Float(descent),
306+
fontBottom: -Float(iosFont.descender),
307+
fontTop: -Float(iosFont.ascender),
308+
fontSize: fontSize.floatValue,
309+
maxFontSize: Float(fLineHeight))
310+
attributes.setObject(baseOffset, forKey:NSAttributedString.Key.baselineOffset as NSCopying as NSCopying)
311+
312+
}
296313
}
297314
attributes.setObject(paragraphStyle, forKey:NSAttributedString.Key.paragraphStyle as NSCopying as NSCopying)
298315
}

src/text/index.android.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ function spanToNativeString(span, parent: any, parentView: any, maxFontSize?, in
5858
if (text && textTransform != null && textTransform !== 'none') {
5959
text = getTransformedText(text, textTransform);
6060
}
61+
let lineHeightFactor = density;
6162
if (!density) {
6263
// if (!FONT_SIZE_FACTOR) {
6364
// FONT_SIZE_FACTOR = com.nativescript.text.Font.getFontSizeFactor(Utils.android.getApplicationContext()) / Screen.mainScreen.scale;
@@ -68,6 +69,9 @@ function spanToNativeString(span, parent: any, parentView: any, maxFontSize?, in
6869
// console.log('text FONT_SIZE_FACTOR', FONT_SIZE_FACTOR, Screen.mainScreen.scale)
6970
verticalTextAlignment = span.verticalAlignment || parent?.verticalAlignment;
7071
}
72+
if (density === 1) {
73+
lineHeightFactor = com.nativescript.text.Font.getFontSizeFactor(Utils.android.getApplicationContext());
74+
}
7175
let backgroundColor = span.backgroundColor || parent?.backgroundColor;
7276
if (backgroundColor && !(backgroundColor instanceof Color)) {
7377
backgroundColor = new Color(backgroundColor);
@@ -97,7 +101,7 @@ function spanToNativeString(span, parent: any, parentView: any, maxFontSize?, in
97101
relativeSize: span.relativeSize,
98102
verticalTextAlignment,
99103
linkColor: aLinkColor,
100-
lineHeight: lineHeight !== undefined ? lineHeight * density : undefined,
104+
lineHeight: lineHeight !== undefined ? lineHeight * lineHeightFactor : undefined,
101105
letterSpacing: letterSpacing !== undefined ? letterSpacing * density : undefined,
102106
color: color ? color.android : undefined,
103107
disableLinkDecoration: (span.linkDecoration && span.linkDecoration !== 'underline') || parentView?.['linkUnderline'] === false,

0 commit comments

Comments
 (0)