Skip to content

Commit d0c58d5

Browse files
committed
fix: use @nativescript-community/text
1 parent f78b6bf commit d0c58d5

File tree

5 files changed

+79
-39
lines changed

5 files changed

+79
-39
lines changed

Diff for: package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,17 @@
5454
"@angular/platform-browser": "~10.1.0",
5555
"@angular/platform-browser-dynamic": "~10.1.0",
5656
"@angular/router": "~10.1.0",
57+
"@commitlint/cli": "^9.1.2",
58+
"@commitlint/config-conventional": "^9.1.2",
59+
"@nativescript-community/text": "^1.2.5",
5760
"@nativescript/angular": "10.1.0",
5861
"@nativescript/core": "7.0.0",
5962
"@nativescript/types-android": "7.0.2",
6063
"@nativescript/types-ios": "7.0.1",
6164
"@nativescript/webpack": "3.0.1",
65+
"@types/node": "^14.6.4",
6266
"@typescript-eslint/eslint-plugin": "4.0.1",
6367
"@typescript-eslint/parser": "4.0.1",
64-
"@commitlint/cli": "^9.1.2",
65-
"@commitlint/config-conventional": "^9.1.2",
66-
"@types/node": "^14.6.4",
6768
"eslint": "7.5.0",
6869
"husky": "^4.2.5",
6970
"lerna": "^3.22.1",

Diff for: plugin/package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@
2626
},
2727
"license": "Apache-2.0",
2828
"homepage": "https://github.com/nativescript-community/ui-label",
29-
"readmeFilename": "README.md"
30-
}
29+
"readmeFilename": "README.md",
30+
"dependencies": {
31+
"@nativescript-community/text": "^1.2.5"
32+
}
33+
}

Diff for: src/label-common.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { dip } from '@nativescript/core/ui/core/view';
1717
import { TextAlignment } from '@nativescript/core/ui/text-base';
1818
import { layout } from '@nativescript/core/utils/utils';
1919
import { Label as LabelViewDefinition, LineBreak, TextShadow } from './label';
20+
import { VerticalTextAlignment } from '@nativescript-community/text';
2021

2122
declare module '@nativescript/core/ui/text-base/formatted-string' {
2223
interface FormattedString {
@@ -149,15 +150,5 @@ export const textShadowProperty = new CssProperty<Style, string | TextShadow>({
149150
});
150151
textShadowProperty.register(Style);
151152

152-
export type VerticalTextAlignment = 'initial' | 'top' | 'middle' | 'bottom' | 'center';
153-
154-
export const verticalTextAlignmentConverter = makeParser<VerticalTextAlignment>(makeValidator<VerticalTextAlignment>('initial', 'top', 'middle', 'bottom', 'center'));
155153
export const textAlignmentConverter = makeParser<TextAlignment>(makeValidator<TextAlignment>('initial', 'left', 'right', 'center'));
156154

157-
export const verticalTextAlignmentProperty = new InheritedCssProperty<Style, VerticalTextAlignment>({
158-
name: 'verticalTextAlignment',
159-
cssName: 'vertical-text-align',
160-
defaultValue: 'initial',
161-
valueConverter: verticalTextAlignmentConverter,
162-
});
163-
verticalTextAlignmentProperty.register(Style);

Diff for: src/label.android.ts

+66-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
import { CSSType, FormattedString, Observable, Property, PropertyChangeData, Span, View, ViewBase, booleanConverter, knownFolders, path, profile } from '@nativescript/core';
1+
import { VerticalTextAlignment, verticalTextAlignmentProperty } from '@nativescript-community/text';
2+
import {
3+
CSSType,
4+
FormattedString,
5+
Observable,
6+
Property,
7+
PropertyChangeData,
8+
Span,
9+
View,
10+
ViewBase,
11+
booleanConverter,
12+
knownFolders,
13+
path,
14+
profile,
15+
} from '@nativescript/core';
216
import { android as androidApp } from '@nativescript/core/application';
317
import { Color } from '@nativescript/core/color';
418
import { Font, FontStyle, FontWeight } from '@nativescript/core/ui/styling/font';
@@ -24,36 +38,39 @@ import {
2438
textTransformProperty,
2539
whiteSpaceProperty,
2640
} from '@nativescript/core/ui/text-base';
27-
import {
28-
lineHeightProperty
29-
} from '@nativescript/core/ui/text-base/text-base-common';
41+
import { lineHeightProperty } from '@nativescript/core/ui/text-base/text-base-common';
3042
import { layout } from '@nativescript/core/utils/utils';
3143
import { Label as LabelViewDefinition, LineBreak, TextShadow } from './label';
3244
import {
33-
VerticalTextAlignment,
3445
cssProperty,
3546
lineBreakProperty,
3647
maxLinesProperty,
3748
needFormattedStringComputation,
3849
textAlignmentConverter,
3950
textShadowProperty,
40-
verticalTextAlignmentProperty,
4151
} from './label-common';
4252

4353
export function enableIOSDTCoreText() {} //unused
4454

4555
let context;
4656
const fontPath = path.join(knownFolders.currentApp().path, 'fonts');
4757

48-
Font.prototype.getAndroidTypeface = function () {
58+
Font.prototype.getAndroidTypeface = profile('getAndroidTypeface', function () {
4959
if (!this._typeface) {
5060
if (!context) {
5161
context = androidApp.context;
5262
}
53-
this._typeface = (com as any).nativescript.label.Font.createTypeface(context, fontPath, this.fontFamily, this.fontWeight, this.isBold, this.isItalic);
63+
this._typeface = (com as any).nativescript.label.Font.createTypeface(
64+
context,
65+
fontPath,
66+
this.fontFamily,
67+
this.fontWeight,
68+
this.isBold,
69+
this.isItalic
70+
);
5471
}
5572
return this._typeface;
56-
};
73+
});
5774

5875
declare module '@nativescript/core/ui/text-base/formatted-string' {
5976
interface FormattedString {
@@ -104,9 +121,11 @@ Span.prototype.toNativeString = function () {
104121
text = getTransformedText(text, textTransform);
105122
}
106123
const delimiter = String.fromCharCode(0x1e);
107-
const result = `${this.fontFamily || 0}${delimiter}${this.fontSize !== undefined ? this.fontSize : -1}${delimiter}${this.fontWeight || ''}${delimiter}${
108-
this.fontStyle === 'italic' ? 1 : 0
109-
}${delimiter}${textDecoration || 0}${delimiter}${this.color ? this.color.android : -1}${delimiter}${backgroundColor ? backgroundColor.android : -1}${delimiter}${this.text}`;
124+
const result = `${this.fontFamily || 0}${delimiter}${this.fontSize !== undefined ? this.fontSize : -1}${delimiter}${
125+
this.fontWeight || ''
126+
}${delimiter}${this.fontStyle === 'italic' ? 1 : 0}${delimiter}${textDecoration || 0}${delimiter}${
127+
this.color ? this.color.android : -1
128+
}${delimiter}${backgroundColor ? backgroundColor.android : -1}${delimiter}${this.text}`;
110129
return result;
111130
};
112131

@@ -139,7 +158,11 @@ declare module '@nativescript/core/ui/core/view-base' {
139158
}
140159

141160
const textProperty = new Property<Label, string>({ name: 'text', defaultValue: '', affectsLayout: true });
142-
const formattedTextProperty = new Property<Label, FormattedString>({ name: 'formattedText', affectsLayout: true, valueChanged: onFormattedTextPropertyChanged });
161+
const formattedTextProperty = new Property<Label, FormattedString>({
162+
name: 'formattedText',
163+
affectsLayout: true,
164+
valueChanged: onFormattedTextPropertyChanged,
165+
});
143166
export const htmlProperty = new Property<Label, string>({ name: 'html', defaultValue: null, affectsLayout: true });
144167

145168
export function buildHTMLString(data: {
@@ -337,11 +360,17 @@ export class Label extends LabelBase {
337360
}
338361
}
339362
[textShadowProperty.setNative](value: TextShadow) {
340-
this.nativeViewProtected.setShadowLayer(layout.toDevicePixels(value.blurRadius), layout.toDevicePixels(value.offsetX), layout.toDevicePixels(value.offsetY), value.color.android);
363+
this.nativeViewProtected.setShadowLayer(
364+
layout.toDevicePixels(value.blurRadius),
365+
layout.toDevicePixels(value.offsetX),
366+
layout.toDevicePixels(value.offsetY),
367+
value.color.android
368+
);
341369
}
342370

343371
[verticalTextAlignmentProperty.setNative](value: VerticalTextAlignment) {
344372
const horizontalGravity = this.nativeTextViewProtected.getGravity() & android.view.Gravity.HORIZONTAL_GRAVITY_MASK;
373+
console.log('verticalTextAlignmentProperty', value, horizontalGravity);
345374
switch (value) {
346375
case 'initial':
347376
case 'top':
@@ -433,7 +462,9 @@ export class Label extends LabelBase {
433462
this.nativeTextViewProtected.setPaintFlags(android.graphics.Paint.STRIKE_THRU_TEXT_FLAG);
434463
break;
435464
case 'underline line-through':
436-
this.nativeTextViewProtected.setPaintFlags(android.graphics.Paint.UNDERLINE_TEXT_FLAG | android.graphics.Paint.STRIKE_THRU_TEXT_FLAG);
465+
this.nativeTextViewProtected.setPaintFlags(
466+
android.graphics.Paint.UNDERLINE_TEXT_FLAG | android.graphics.Paint.STRIKE_THRU_TEXT_FLAG
467+
);
437468
break;
438469
default:
439470
this.nativeTextViewProtected.setPaintFlags(value);
@@ -449,28 +480,40 @@ export class Label extends LabelBase {
449480
return { value: this._defaultPaddingTop, unit: 'px' };
450481
}
451482
[paddingTopProperty.setNative](value: Length) {
452-
org.nativescript.widgets.ViewHelper.setPaddingTop(this.nativeTextViewProtected, Length.toDevicePixels(value, 0) + Length.toDevicePixels(this.style.borderTopWidth, 0));
483+
org.nativescript.widgets.ViewHelper.setPaddingTop(
484+
this.nativeTextViewProtected,
485+
Length.toDevicePixels(value, 0) + Length.toDevicePixels(this.style.borderTopWidth, 0)
486+
);
453487
}
454488

455489
[paddingRightProperty.getDefault](): Length {
456490
return { value: this._defaultPaddingRight, unit: 'px' };
457491
}
458492
[paddingRightProperty.setNative](value: Length) {
459-
org.nativescript.widgets.ViewHelper.setPaddingRight(this.nativeTextViewProtected, Length.toDevicePixels(value, 0) + Length.toDevicePixels(this.style.borderRightWidth, 0));
493+
org.nativescript.widgets.ViewHelper.setPaddingRight(
494+
this.nativeTextViewProtected,
495+
Length.toDevicePixels(value, 0) + Length.toDevicePixels(this.style.borderRightWidth, 0)
496+
);
460497
}
461498

462499
[paddingBottomProperty.getDefault](): Length {
463500
return { value: this._defaultPaddingBottom, unit: 'px' };
464501
}
465502
[paddingBottomProperty.setNative](value: Length) {
466-
org.nativescript.widgets.ViewHelper.setPaddingBottom(this.nativeTextViewProtected, Length.toDevicePixels(value, 0) + Length.toDevicePixels(this.style.borderBottomWidth, 0));
503+
org.nativescript.widgets.ViewHelper.setPaddingBottom(
504+
this.nativeTextViewProtected,
505+
Length.toDevicePixels(value, 0) + Length.toDevicePixels(this.style.borderBottomWidth, 0)
506+
);
467507
}
468508

469509
[paddingLeftProperty.getDefault](): Length {
470510
return { value: this._defaultPaddingLeft, unit: 'px' };
471511
}
472512
[paddingLeftProperty.setNative](value: Length) {
473-
org.nativescript.widgets.ViewHelper.setPaddingLeft(this.nativeTextViewProtected, Length.toDevicePixels(value, 0) + Length.toDevicePixels(this.style.borderLeftWidth, 0));
513+
org.nativescript.widgets.ViewHelper.setPaddingLeft(
514+
this.nativeTextViewProtected,
515+
Length.toDevicePixels(value, 0) + Length.toDevicePixels(this.style.borderLeftWidth, 0)
516+
);
474517
}
475518

476519
@profile
@@ -498,7 +541,10 @@ export class Label extends LabelBase {
498541
textProperty.nativeValueChange(this, this.html === null || this.html === undefined ? '' : this.html);
499542
} else if (this.formattedText) {
500543
transformedText = this.createSpannableStringBuilder();
501-
textProperty.nativeValueChange(this, this.formattedText === null || this.formattedText === undefined ? '' : this.formattedText.toString());
544+
textProperty.nativeValueChange(
545+
this,
546+
this.formattedText === null || this.formattedText === undefined ? '' : this.formattedText.toString()
547+
);
502548
} else {
503549
const text = this.text;
504550
const stringValue = text === null || text === undefined ? '' : text.toString();

Diff for: src/label.ios.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { VerticalTextAlignment, verticalTextAlignmentProperty } from '@nativescript-community/text';
12
import { Color, View } from '@nativescript/core';
23
import {
34
Length,
@@ -11,18 +12,17 @@ import {
1112
paddingRightProperty,
1213
paddingTopProperty,
1314
} from '@nativescript/core/ui/styling/style-properties';
14-
15-
import { lineHeightProperty } from '@nativescript/core/ui/text-base/text-base-common';
1615
import {
1716
TextAlignment,
1817
TextTransform,
1918
WhiteSpace,
2019
letterSpacingProperty,
2120
whiteSpaceProperty,
2221
} from '@nativescript/core/ui/text-base';
22+
import { lineHeightProperty } from '@nativescript/core/ui/text-base/text-base-common';
2323
import { isString } from '@nativescript/core/utils/types';
2424
import { layout } from '@nativescript/core/utils/utils';
25-
import { TextShadow, VerticalTextAlignment } from './label';
25+
import { TextShadow } from './label';
2626
import {
2727
LabelBase,
2828
htmlProperty,
@@ -31,7 +31,6 @@ import {
3131
needFormattedStringComputation,
3232
textAlignmentConverter,
3333
textShadowProperty,
34-
verticalTextAlignmentProperty,
3534
} from './label-common';
3635

3736
export * from './label-common';

0 commit comments

Comments
 (0)