1
- import { VerticalTextAlignment , createNativeAttributedString , verticalTextAlignmentProperty } from '@nativescript-community/text' ;
2
- import { Color , View } from '@nativescript/core' ;
1
+ import { VerticalTextAlignment , computeBaseLineOffset , createNativeAttributedString , verticalTextAlignmentProperty } from '@nativescript-community/text' ;
2
+ import { Color , Font , FormattedString , Span , View } from '@nativescript/core' ;
3
3
import {
4
4
Length ,
5
5
borderBottomWidthProperty ,
@@ -14,12 +14,15 @@ import {
14
14
} from '@nativescript/core/ui/styling/style-properties' ;
15
15
import {
16
16
TextAlignment ,
17
+ TextBase ,
18
+ TextDecoration ,
17
19
TextTransform ,
18
20
WhiteSpace ,
19
21
letterSpacingProperty ,
22
+ textDecorationProperty ,
20
23
whiteSpaceProperty ,
21
24
} from '@nativescript/core/ui/text-base' ;
22
- import { lineHeightProperty } from '@nativescript/core/ui/text-base/text-base-common' ;
25
+ import { getClosestPropertyValue , lineHeightProperty } from '@nativescript/core/ui/text-base/text-base-common' ;
23
26
import { isNullOrUndefined , isString } from '@nativescript/core/utils/types' ;
24
27
import { iOSNativeHelper , layout } from '@nativescript/core/utils/utils' ;
25
28
import { TextShadow } from './label' ;
@@ -50,6 +53,8 @@ declare module '@nativescript/core/ui/text-base' {
50
53
interface TextBase {
51
54
_requestLayoutOnTextChanged ( ) ;
52
55
_setNativeText ( ) ;
56
+ createMutableStringForSpan ?( span , text ) : NSMutableAttributedString ;
57
+ createNSMutableAttributedString ?( formattedString : FormattedString ) : NSMutableAttributedString ;
53
58
// createNSMutableAttributedString(formattedString: FormattedString);
54
59
}
55
60
}
@@ -579,6 +584,68 @@ export class Label extends LabelBase {
579
584
( this as any ) . _setColor ( UIColor . labelColor ) ;
580
585
}
581
586
}
587
+ currentMaxFontSize = 0 ;
588
+
589
+ createNSMutableAttributedString ( formattedString : FormattedString ) : NSMutableAttributedString {
590
+ // we need to store the max Font size to pass it to createMutableStringForSpan
591
+ const length = formattedString . spans . length ;
592
+ let maxFontSize = formattedString . style ?. fontSize || this ?. style . fontSize || 0 ;
593
+ for ( let i = 0 ; i < length ; i ++ ) {
594
+ const s = formattedString . spans . getItem ( i ) ;
595
+ if ( s . style . fontSize ) {
596
+ maxFontSize = Math . max ( maxFontSize , s . style . fontSize ) ;
597
+ }
598
+ }
599
+ this . currentMaxFontSize = maxFontSize ;
600
+ return super . createNSMutableAttributedString ( formattedString ) ;
601
+ }
602
+ createMutableStringForSpan ( span : Span , text : string ) : NSMutableAttributedString {
603
+ const viewFont = this . nativeTextViewProtected . font ;
604
+ const attrDict : { key : string ; value : any } = { } as any ;
605
+ const style = span . style ;
606
+
607
+ let align = style . verticalAlignment || ( span . parent as FormattedString ) . style . verticalAlignment ;
608
+ if ( ! align || align === 'stretch' ) {
609
+ align = this . verticalTextAlignment as any ;
610
+ }
611
+ const font = new Font ( style . fontFamily , style . fontSize , style . fontStyle , style . fontWeight ) ;
612
+ const iosFont = font . getUIFont ( viewFont ) ;
613
+
614
+ attrDict [ NSFontAttributeName ] = iosFont ;
615
+ if ( span . color ) {
616
+ const color = span . color instanceof Color ? span . color : new Color ( span . color as any ) ;
617
+ attrDict [ NSForegroundColorAttributeName ] = color . ios ;
618
+ }
619
+
620
+ // We don't use isSet function here because defaultValue for backgroundColor is null.
621
+ const backgroundColor : Color = ( style . backgroundColor || ( span . parent as FormattedString ) . backgroundColor || ( span . parent . parent as TextBase ) . backgroundColor ) as Color ;
622
+ if ( backgroundColor ) {
623
+ const color = backgroundColor instanceof Color ? backgroundColor : new Color ( backgroundColor ) ;
624
+ attrDict [ NSBackgroundColorAttributeName ] = color . ios ;
625
+ }
626
+
627
+ const textDecoration : TextDecoration = getClosestPropertyValue ( textDecorationProperty , span ) ;
628
+
629
+ if ( textDecoration ) {
630
+ const underline = textDecoration . indexOf ( 'underline' ) !== - 1 ;
631
+ if ( underline ) {
632
+ attrDict [ NSUnderlineStyleAttributeName ] = underline ;
633
+ }
634
+
635
+ const strikethrough = textDecoration . indexOf ( 'line-through' ) !== - 1 ;
636
+ if ( strikethrough ) {
637
+ attrDict [ NSStrikethroughStyleAttributeName ] = strikethrough ;
638
+ }
639
+ }
640
+
641
+ if ( align && align !== 'stretch' ) {
642
+ if ( iosFont ) {
643
+ attrDict [ NSBaselineOffsetAttributeName ] = - computeBaseLineOffset ( align , - iosFont . ascender , - iosFont . descender , - iosFont . ascender , - iosFont . descender , iosFont . pointSize , this . currentMaxFontSize ) ;
644
+ }
645
+ }
646
+
647
+ return NSMutableAttributedString . alloc ( ) . initWithStringAttributes ( text , attrDict as any ) ;
648
+ }
582
649
[ paddingTopProperty . getDefault ] ( ) : Length {
583
650
return {
584
651
value : 0 ,
0 commit comments