|
1 |
| -import { Color, getTransformedText } from '@nativescript/core'; |
2 |
| -import { Font } from '@nativescript/core/ui/styling/font'; |
3 |
| -import { computeBaseLineOffset, createNativeAttributedString, createSpannable } from '@nativescript-community/text'; |
| 1 | +import { createNativeAttributedString, createSpannable } from '@nativescript-community/text'; |
4 | 2 | import { CanvasLabel as CanvasLabelBase, Group as GroupBase, Span as SpanBase } from './canvaslabel.common';
|
5 | 3 |
|
6 |
| -// export function createSpannable(span: Span, parentCanvas: CanvasLabelBase, parent?: Group, maxFontSize?): NSMutableAttributedString { |
7 |
| -// let text = span.text; |
8 |
| -// if (!text || span.visibility !== 'visible') { |
9 |
| -// return null; |
10 |
| -// } |
11 |
| -// const attrDict = {} as { key: string; value: any }; |
12 |
| -// const fontFamily = span.fontFamily; |
13 |
| -// const fontSize = span.fontSize; |
14 |
| -// const realMaxFontSize = Math.max(maxFontSize, parentCanvas.fontSize || 0); |
15 |
| -// const fontweight = span.fontWeight; |
16 |
| -// const fontstyle = span.fontStyle; |
17 |
| -// const textcolor = span.color; |
18 |
| -// const backgroundcolor = span.backgroundColor || (parent && parent.backgroundColor); |
19 |
| -// const textDecorations = span.textDecoration || (parent && parent.textDecoration); |
20 |
| -// const letterSpacing = span.letterSpacing || (parent && parent.letterSpacing); |
21 |
| -// const lineHeight = span.lineHeight || (parent && parent.lineHeight); |
22 |
| -// const textAlignment = span.textAlignment || (parent && parent.textAlignment); |
23 |
| -// const verticaltextalignment = span.verticalTextAlignment; |
24 |
| -// let iosFont: UIFont; |
25 |
| -// if (fontweight || fontstyle || fontFamily || fontSize) { |
26 |
| -// const font = new Font( |
27 |
| -// fontFamily || (parent && parent.fontFamily) || (parentCanvas && parentCanvas.fontFamily), |
28 |
| -// fontSize || (parent && parent.fontSize) || (parentCanvas && parentCanvas.fontSize), |
29 |
| -// fontstyle || (parent && parent.fontStyle) || ((parentCanvas && parentCanvas.fontStyle) as any), |
30 |
| -// fontweight || (parent && parent.fontWeight) || ((parentCanvas && parentCanvas.fontWeight) as any) |
31 |
| -// ); |
32 |
| -// iosFont = font.getUIFont(UIFont.systemFontOfSize(fontSize)); |
33 |
| -// attrDict[NSFontAttributeName] = iosFont; |
34 |
| -// } |
35 |
| -// if (verticaltextalignment && verticaltextalignment !== 'initial' && iosFont) { |
36 |
| -// const ascent = CTFontGetAscent(iosFont); |
37 |
| -// const descent = CTFontGetDescent(iosFont); |
38 |
| -// attrDict[NSBaselineOffsetAttributeName] = -computeBaseLineOffset(verticaltextalignment, -ascent, descent, -iosFont.descender, -iosFont.ascender, fontSize, realMaxFontSize); |
39 |
| -// } |
40 |
| -// // if (span.mTappable) { |
41 |
| -// // attrDict[NSLinkAttributeName] = text; |
42 |
| -// // } |
43 |
| -// if (textcolor) { |
44 |
| -// const color = textcolor instanceof Color ? textcolor : new Color(textcolor as any); |
45 |
| -// attrDict[NSForegroundColorAttributeName] = color.ios; |
46 |
| -// } |
47 |
| - |
48 |
| -// if (backgroundcolor) { |
49 |
| -// const color = backgroundcolor instanceof Color ? backgroundcolor : new Color(backgroundcolor as any); |
50 |
| -// attrDict[NSBackgroundColorAttributeName] = color.ios; |
51 |
| -// } |
52 |
| -// if (letterSpacing) { |
53 |
| -// attrDict[NSKernAttributeName] = letterSpacing * iosFont.pointSize; |
54 |
| -// } |
55 |
| - |
56 |
| -// let paragraphStyle; |
57 |
| -// if (lineHeight !== undefined) { |
58 |
| -// paragraphStyle = NSMutableParagraphStyle.alloc().init(); |
59 |
| -// switch (textAlignment) { |
60 |
| -// case 'middle': |
61 |
| -// case 'center': |
62 |
| -// paragraphStyle.alignment = NSTextAlignment.Center; |
63 |
| -// break; |
64 |
| -// case 'right': |
65 |
| -// paragraphStyle.alignment = NSTextAlignment.Right; |
66 |
| -// break; |
67 |
| -// default: |
68 |
| -// paragraphStyle.alignment = NSTextAlignment.Left; |
69 |
| -// break; |
70 |
| -// } |
71 |
| -// paragraphStyle.minimumLineHeight = lineHeight; |
72 |
| -// paragraphStyle.maximumLineHeight = lineHeight; |
73 |
| -// } |
74 |
| -// if (paragraphStyle) { |
75 |
| -// attrDict[NSParagraphStyleAttributeName] = paragraphStyle; |
76 |
| -// } |
77 |
| - |
78 |
| -// if (textDecorations) { |
79 |
| -// const underline = textDecorations.indexOf('underline') !== -1; |
80 |
| -// if (underline) { |
81 |
| -// attrDict[NSUnderlineStyleAttributeName] = underline; |
82 |
| -// } |
83 |
| - |
84 |
| -// const strikethrough = textDecorations.indexOf('line-through') !== -1; |
85 |
| -// if (strikethrough) { |
86 |
| -// attrDict[NSStrikethroughStyleAttributeName] = strikethrough; |
87 |
| -// } |
88 |
| -// } |
89 |
| - |
90 |
| -// if (!(text instanceof NSAttributedString)) { |
91 |
| -// if (!(typeof text === 'string')) { |
92 |
| -// text = text.toString(); |
93 |
| -// } |
94 |
| -// if (text.indexOf('\n') !== -1) { |
95 |
| -// text = text.replace(/\\n/g, '\u{2029}'); |
96 |
| -// } |
97 |
| -// const textTransform = span.textTransform || (parent && parent.textTransform); |
98 |
| -// if (textTransform) { |
99 |
| -// text = getTransformedText(text, textTransform); |
100 |
| -// } |
101 |
| -// return NSMutableAttributedString.alloc().initWithStringAttributes(text, attrDict as any); |
102 |
| -// } else { |
103 |
| -// const result = NSMutableAttributedString.alloc().initWithAttributedString(text); |
104 |
| -// result.setAttributesRange(attrDict as any, { location: 0, length: text.length }); |
105 |
| -// return result; |
106 |
| -// } |
107 |
| -// } |
108 |
| - |
109 | 4 | export class Span extends SpanBase {
|
110 | 5 | createNative(parentCanvas: CanvasLabelBase, parent?: Group, maxFontSize?: number) {
|
111 | 6 | this.mNative = createSpannable(this, parentCanvas, parent, maxFontSize);
|
|
0 commit comments