8
8
} from '@nativescript-community/text' ;
9
9
import {
10
10
CSSType ,
11
+ CoreTypes ,
11
12
FormattedString ,
12
13
Observable ,
13
14
Property ,
14
15
PropertyChangeData ,
15
16
Span ,
16
17
View ,
17
- ViewBase ,
18
18
booleanConverter ,
19
19
profile
20
20
} from '@nativescript/core' ;
21
21
import { Color } from '@nativescript/core/color' ;
22
+ import { CSSShadow } from '@nativescript/core/ui/styling/css-shadow' ;
22
23
import { Font , FontStyle , FontWeight } from '@nativescript/core/ui/styling/font' ;
23
24
import {
24
25
Length ,
@@ -31,10 +32,6 @@ import {
31
32
paddingTopProperty
32
33
} from '@nativescript/core/ui/styling/style-properties' ;
33
34
import {
34
- TextAlignment ,
35
- TextDecoration ,
36
- TextTransform ,
37
- WhiteSpace ,
38
35
letterSpacingProperty ,
39
36
textAlignmentProperty ,
40
37
textDecorationProperty ,
@@ -48,13 +45,11 @@ import {
48
45
autoFontSizeProperty ,
49
46
lineBreakProperty ,
50
47
maxLinesProperty ,
51
- needFormattedStringComputation ,
52
48
selectableProperty ,
53
49
textShadowProperty
54
50
} from './label-common' ;
55
51
56
- export { enableIOSDTCoreText , createNativeAttributedString } from '@nativescript-community/text' ;
57
-
52
+ export { createNativeAttributedString , enableIOSDTCoreText } from '@nativescript-community/text' ;
58
53
export * from './label-common' ;
59
54
60
55
let TextView : typeof com . nativescript . label . EllipsizingTextView ;
@@ -98,7 +93,7 @@ export const htmlProperty = new Property<Label, string>({ name: 'html', defaultV
98
93
99
94
type ClickableSpan = new ( owner : Span ) => android . text . style . ClickableSpan ;
100
95
101
- function getHorizontalGravity ( textAlignment : TextAlignment ) {
96
+ function getHorizontalGravity ( textAlignment : CoreTypes . TextAlignmentType ) {
102
97
switch ( textAlignment ) {
103
98
case 'initial' :
104
99
case 'left' :
@@ -207,6 +202,7 @@ abstract class LabelBase extends View implements LabelViewDefinition {
207
202
@cssProperty maxFontSize : number ;
208
203
@cssProperty verticalTextAlignment : VerticalTextAlignment ;
209
204
@cssProperty linkColor : Color ;
205
+ @cssProperty textShadow : CSSShadow ;
210
206
@cssProperty linkUnderline : boolean ;
211
207
public html : string ;
212
208
@cssProperty selectable : boolean ;
@@ -230,16 +226,16 @@ abstract class LabelBase extends View implements LabelViewDefinition {
230
226
@cssProperty letterSpacing : number ;
231
227
@cssProperty lineHeight : number ;
232
228
@cssProperty lineBreak : LineBreak ;
233
- @cssProperty textAlignment : TextAlignment ;
234
- @cssProperty textDecoration : TextDecoration ;
235
- @cssProperty textTransform : TextTransform ;
236
- @cssProperty whiteSpace : WhiteSpace ;
229
+ @cssProperty textAlignment : CoreTypes . TextAlignmentType ;
230
+ @cssProperty textDecoration : CoreTypes . TextDecorationType ;
231
+ @cssProperty textTransform : CoreTypes . TextTransformType ;
232
+ @cssProperty whiteSpace : CoreTypes . WhiteSpaceType ;
237
233
238
- @cssProperty padding : string | Length ;
239
- @cssProperty paddingTop : Length ;
240
- @cssProperty paddingRight : Length ;
241
- @cssProperty paddingBottom : Length ;
242
- @cssProperty paddingLeft : Length ;
234
+ @cssProperty padding : string | CoreTypes . LengthType ;
235
+ @cssProperty paddingTop : CoreTypes . LengthType ;
236
+ @cssProperty paddingRight : CoreTypes . LengthType ;
237
+ @cssProperty paddingBottom : CoreTypes . LengthType ;
238
+ @cssProperty paddingLeft : CoreTypes . LengthType ;
243
239
244
240
// for now code is duplicated as Android version is a full rewrite
245
241
_canChangeText = true ;
@@ -358,7 +354,7 @@ export class Label extends LabelBase {
358
354
}
359
355
}
360
356
361
- [ whiteSpaceProperty . setNative ] ( value : WhiteSpace ) {
357
+ [ whiteSpaceProperty . setNative ] ( value : CoreTypes . WhiteSpaceType ) {
362
358
if ( ! this . lineBreak ) {
363
359
const nativeView = this . nativeTextViewProtected ;
364
360
switch ( value ) {
@@ -374,11 +370,21 @@ export class Label extends LabelBase {
374
370
}
375
371
}
376
372
}
377
- [ textShadowProperty . setNative ] ( value : TextShadow ) {
373
+ [ textShadowProperty . getDefault ] ( value : number ) {
374
+ return {
375
+ radius : this . nativeTextViewProtected . getShadowRadius ( ) ,
376
+ offsetX : this . nativeTextViewProtected . getShadowDx ( ) ,
377
+ offsetY : this . nativeTextViewProtected . getShadowDy ( ) ,
378
+ color : this . nativeTextViewProtected . getShadowColor ( )
379
+ } ;
380
+ }
381
+
382
+ [ textShadowProperty . setNative ] ( value : CSSShadow ) {
383
+ // prettier-ignore
378
384
this . nativeViewProtected . setShadowLayer (
379
- layout . toDevicePixels ( value . blurRadius ) ,
380
- layout . toDevicePixels ( value . offsetX ) ,
381
- layout . toDevicePixels ( value . offsetY ) ,
385
+ Length . toDevicePixels ( value . blurRadius , java . lang . Float . MIN_VALUE ) ,
386
+ Length . toDevicePixels ( value . offsetX , 0 ) ,
387
+ Length . toDevicePixels ( value . offsetY , 0 ) ,
382
388
value . color . android
383
389
) ;
384
390
}
@@ -404,11 +410,11 @@ export class Label extends LabelBase {
404
410
this . _setNativeText ( ) ;
405
411
}
406
412
407
- [ textTransformProperty . setNative ] ( value : TextTransform ) {
413
+ [ textTransformProperty . setNative ] ( value : CoreTypes . TextTransformType ) {
408
414
this . _setNativeText ( ) ;
409
415
}
410
416
411
- [ textAlignmentProperty . setNative ] ( value : TextAlignment ) {
417
+ [ textAlignmentProperty . setNative ] ( value : CoreTypes . TextAlignmentType ) {
412
418
const view = this . nativeTextViewProtected ;
413
419
view . setGravity ( getHorizontalGravity ( value ) | getVerticalGravity ( this . verticalTextAlignment ) ) ;
414
420
}
@@ -436,7 +442,7 @@ export class Label extends LabelBase {
436
442
this . nativeTextViewProtected . setTypeface ( value instanceof Font ? value . getAndroidTypeface ( ) : value ) ;
437
443
}
438
444
439
- [ textDecorationProperty . setNative ] ( value : number | TextDecoration ) {
445
+ [ textDecorationProperty . setNative ] ( value : number | CoreTypes . TextDecorationType ) {
440
446
switch ( value ) {
441
447
case 'none' :
442
448
this . nativeTextViewProtected . setPaintFlags ( 0 ) ;
@@ -462,40 +468,40 @@ export class Label extends LabelBase {
462
468
org . nativescript . widgets . ViewHelper . setLetterspacing ( this . nativeTextViewProtected , value ) ;
463
469
}
464
470
465
- [ paddingTopProperty . getDefault ] ( ) : Length {
471
+ [ paddingTopProperty . getDefault ] ( ) : CoreTypes . LengthType {
466
472
return { value : this . _defaultPaddingTop , unit : 'px' } ;
467
473
}
468
- [ paddingTopProperty . setNative ] ( value : Length ) {
474
+ [ paddingTopProperty . setNative ] ( value : CoreTypes . LengthType ) {
469
475
org . nativescript . widgets . ViewHelper . setPaddingTop (
470
476
this . nativeTextViewProtected ,
471
477
Length . toDevicePixels ( value , 0 ) + Length . toDevicePixels ( this . style . borderTopWidth , 0 )
472
478
) ;
473
479
}
474
480
475
- [ paddingRightProperty . getDefault ] ( ) : Length {
481
+ [ paddingRightProperty . getDefault ] ( ) : CoreTypes . LengthType {
476
482
return { value : this . _defaultPaddingRight , unit : 'px' } ;
477
483
}
478
- [ paddingRightProperty . setNative ] ( value : Length ) {
484
+ [ paddingRightProperty . setNative ] ( value : CoreTypes . LengthType ) {
479
485
org . nativescript . widgets . ViewHelper . setPaddingRight (
480
486
this . nativeTextViewProtected ,
481
487
Length . toDevicePixels ( value , 0 ) + Length . toDevicePixels ( this . style . borderRightWidth , 0 )
482
488
) ;
483
489
}
484
490
485
- [ paddingBottomProperty . getDefault ] ( ) : Length {
491
+ [ paddingBottomProperty . getDefault ] ( ) : CoreTypes . LengthType {
486
492
return { value : this . _defaultPaddingBottom , unit : 'px' } ;
487
493
}
488
- [ paddingBottomProperty . setNative ] ( value : Length ) {
494
+ [ paddingBottomProperty . setNative ] ( value : CoreTypes . LengthType ) {
489
495
org . nativescript . widgets . ViewHelper . setPaddingBottom (
490
496
this . nativeTextViewProtected ,
491
497
Length . toDevicePixels ( value , 0 ) + Length . toDevicePixels ( this . style . borderBottomWidth , 0 )
492
498
) ;
493
499
}
494
500
495
- [ paddingLeftProperty . getDefault ] ( ) : Length {
501
+ [ paddingLeftProperty . getDefault ] ( ) : CoreTypes . LengthType {
496
502
return { value : this . _defaultPaddingLeft , unit : 'px' } ;
497
503
}
498
- [ paddingLeftProperty . setNative ] ( value : Length ) {
504
+ [ paddingLeftProperty . setNative ] ( value : CoreTypes . LengthType ) {
499
505
org . nativescript . widgets . ViewHelper . setPaddingLeft (
500
506
this . nativeTextViewProtected ,
501
507
Length . toDevicePixels ( value , 0 ) + Length . toDevicePixels ( this . style . borderLeftWidth , 0 )
@@ -522,35 +528,15 @@ export class Label extends LabelBase {
522
528
[ selectableProperty . setNative ] ( value : boolean ) {
523
529
this . nativeTextViewProtected . setTextIsSelectable ( value ) ;
524
530
}
531
+ createFormattedTextNative ( value : any ) {
532
+ const result = createNativeAttributedString ( value , this ) ;
525
533
526
- @profile
527
- createHTMLString ( ) {
528
- const result = createNativeAttributedString ( { text : this . html } ) as android . text . SpannableStringBuilder ;
529
- const urlSpan = result . getSpans ( 0 , result . length ( ) , android . text . style . URLSpan . class ) ;
530
- if ( urlSpan . length > 0 ) {
531
- this . _setTappableState ( true ) ;
532
- initializeURLClickableSpan ( ) ;
533
- for ( let index = 0 ; index < urlSpan . length ; index ++ ) {
534
- const span = urlSpan [ index ] ;
535
- const text = span . getURL ( ) ;
536
- const start = result . getSpanStart ( span ) ;
537
- const end = result . getSpanEnd ( span ) ;
538
- result . removeSpan ( span ) ;
539
- result . setSpan ( new URLClickableSpan ( text , this ) , start , end , android . text . Spanned . SPAN_EXCLUSIVE_EXCLUSIVE ) ;
540
- }
541
- }
542
- return result ;
543
- }
544
- @profile
545
- createSpannableStringBuilder ( ) {
546
- const formattedText = this . formattedText ;
547
- const result = createNativeAttributedString ( formattedText as any ) ;
548
534
let indexSearch = 0 ;
549
535
let str : string ;
550
- formattedText . spans . forEach ( ( s ) => {
536
+ value . spans . forEach ( ( s ) => {
551
537
if ( s . tappable ) {
552
538
if ( ! str ) {
553
- str = formattedText . toString ( ) ;
539
+ str = value . toString ( ) ;
554
540
this . _setTappableState ( true ) ;
555
541
}
556
542
initializeClickableSpan ( ) ;
@@ -564,6 +550,24 @@ export class Label extends LabelBase {
564
550
} ) ;
565
551
return result ;
566
552
}
553
+ @profile
554
+ createHTMLString ( ) {
555
+ const result = createNativeAttributedString ( { text : this . html } , this ) as android . text . SpannableStringBuilder ;
556
+ const urlSpan = result . getSpans ( 0 , result . length ( ) , android . text . style . URLSpan . class ) ;
557
+ if ( urlSpan . length > 0 ) {
558
+ this . _setTappableState ( true ) ;
559
+ initializeURLClickableSpan ( ) ;
560
+ for ( let index = 0 ; index < urlSpan . length ; index ++ ) {
561
+ const span = urlSpan [ index ] ;
562
+ const text = span . getURL ( ) ;
563
+ const start = result . getSpanStart ( span ) ;
564
+ const end = result . getSpanEnd ( span ) ;
565
+ result . removeSpan ( span ) ;
566
+ result . setSpan ( new URLClickableSpan ( text , this ) , start , end , android . text . Spanned . SPAN_EXCLUSIVE_EXCLUSIVE ) ;
567
+ }
568
+ }
569
+ return result ;
570
+ }
567
571
_tappable = false ;
568
572
_setTappableState ( tappable : boolean ) {
569
573
if ( this . _tappable !== tappable ) {
@@ -596,7 +600,7 @@ export class Label extends LabelBase {
596
600
transformedText = this . createHTMLString ( ) ;
597
601
textProperty . nativeValueChange ( this , this . html === null || this . html === undefined ? '' : this . html ) ;
598
602
} else if ( this . formattedText ) {
599
- transformedText = this . createSpannableStringBuilder ( ) ;
603
+ transformedText = this . createFormattedTextNative ( this . formattedText ) ;
600
604
textProperty . nativeValueChange (
601
605
this ,
602
606
this . formattedText === null || this . formattedText === undefined ? '' : this . formattedText . toString ( )
@@ -659,7 +663,7 @@ function getCapitalizedString(str: string): string {
659
663
return newWords . join ( ' ' ) ;
660
664
}
661
665
662
- export function getTransformedText ( text : string , textTransform : TextTransform ) : string {
666
+ export function getTransformedText ( text : string , textTransform : CoreTypes . TextTransformType ) : string {
663
667
switch ( textTransform ) {
664
668
case 'uppercase' :
665
669
return text . toUpperCase ( ) ;
0 commit comments