Skip to content

Commit e9cf77d

Browse files
committed
feat: better handle of span verticalAlignment
1 parent e476512 commit e9cf77d

File tree

3 files changed

+87
-20
lines changed

3 files changed

+87
-20
lines changed

Diff for: package.json

+16-16
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,28 @@
5555
"@angular/platform-browser": "~10.1.0",
5656
"@angular/platform-browser-dynamic": "~10.1.0",
5757
"@angular/router": "~10.1.0",
58-
"@commitlint/cli": "^9.1.2",
59-
"@commitlint/config-conventional": "^9.1.2",
60-
"@nativescript-community/text": "^1.3.10",
58+
"@commitlint/cli": "^11.0.0",
59+
"@commitlint/config-conventional": "^11.0.0",
60+
"@nativescript-community/text": "^1.4.0",
6161
"@nativescript/angular": "10.1.0",
62-
"@nativescript/core": "7.0.0",
63-
"@nativescript/types-android": "7.0.2",
64-
"@nativescript/types-ios": "7.0.1",
65-
"@nativescript/webpack": "3.0.1",
66-
"@types/node": "^14.6.4",
67-
"@typescript-eslint/eslint-plugin": "4.0.1",
68-
"@typescript-eslint/parser": "4.0.1",
69-
"eslint": "7.5.0",
70-
"husky": "^4.2.5",
62+
"@nativescript/core": "7.2.1",
63+
"@nativescript/types-android": "7.2.0",
64+
"@nativescript/types-ios": "7.2.0",
65+
"@nativescript/webpack": "4.1.0",
66+
"@types/node": "^14.14.27",
67+
"@typescript-eslint/eslint-plugin": "4.15.0",
68+
"@typescript-eslint/parser": "4.15.0",
69+
"eslint": "7.19.0",
70+
"husky": "^4.3.8",
7171
"lerna": "^3.22.1",
7272
"npm-watch": "^0.7.0",
73-
"prompt": "^1.0.0",
73+
"prompt": "^1.1.0",
7474
"rimraf": "^3.0.2",
75-
"ts-patch": "^1.3.0",
76-
"typescript": "~3.9.7"
75+
"ts-patch": "^1.3.2",
76+
"typescript": "~4.1.5"
7777
},
7878
"dependencies": {
79-
"ts-node": "^9.0.0"
79+
"ts-node": "^9.1.1"
8080
},
8181
"bootstrapper": "nativescript-plugin-seed",
8282
"commitlint": {

Diff for: plugin/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
"license": "Apache-2.0",
3333
"readmeFilename": "README.md",
3434
"dependencies": {
35-
"@nativescript-community/text": "^1.3.10"
35+
"@nativescript-community/text": "^1.4.0"
3636
}
3737
}

Diff for: src/label.ios.ts

+70-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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';
33
import {
44
Length,
55
borderBottomWidthProperty,
@@ -14,12 +14,15 @@ import {
1414
} from '@nativescript/core/ui/styling/style-properties';
1515
import {
1616
TextAlignment,
17+
TextBase,
18+
TextDecoration,
1719
TextTransform,
1820
WhiteSpace,
1921
letterSpacingProperty,
22+
textDecorationProperty,
2023
whiteSpaceProperty,
2124
} 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';
2326
import { isNullOrUndefined, isString } from '@nativescript/core/utils/types';
2427
import { iOSNativeHelper, layout } from '@nativescript/core/utils/utils';
2528
import { TextShadow } from './label';
@@ -50,6 +53,8 @@ declare module '@nativescript/core/ui/text-base' {
5053
interface TextBase {
5154
_requestLayoutOnTextChanged();
5255
_setNativeText();
56+
createMutableStringForSpan?(span, text): NSMutableAttributedString;
57+
createNSMutableAttributedString?(formattedString: FormattedString): NSMutableAttributedString;
5358
// createNSMutableAttributedString(formattedString: FormattedString);
5459
}
5560
}
@@ -579,6 +584,68 @@ export class Label extends LabelBase {
579584
(this as any)._setColor(UIColor.labelColor);
580585
}
581586
}
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+
}
582649
[paddingTopProperty.getDefault](): Length {
583650
return {
584651
value: 0,

0 commit comments

Comments
 (0)