forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-base-styler.android.ts
More file actions
168 lines (139 loc) · 6.93 KB
/
text-base-styler.android.ts
File metadata and controls
168 lines (139 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import view = require("ui/core/view");
import utils = require("utils/utils");
import style = require("ui/styling/style");
import font = require("ui/styling/font");
import enums = require("ui/enums");
export class TextBaseStyler implements style.Styler {
// color
private static setColorProperty(view: view.View, newValue: any) {
(<android.widget.TextView>view._nativeView).setTextColor(newValue);
}
private static resetColorProperty(view: view.View, nativeValue: any) {
(<android.widget.TextView>view._nativeView).setTextColor(nativeValue);
}
private static getNativeColorValue(view: view.View): any {
return (<android.widget.TextView>view._nativeView).getTextColors().getDefaultColor();
}
// font
private static setFontInternalProperty(view: view.View, newValue: any, nativeValue?: any) {
var tv = <android.widget.TextView>view._nativeView;
var fontValue = <font.Font>newValue;
var typeface = fontValue.getAndroidTypeface();
if (typeface) {
tv.setTypeface(typeface);
}
else {
tv.setTypeface(nativeValue.typeface);
}
if (fontValue.fontSize) {
tv.setTextSize(fontValue.fontSize);
}
else {
tv.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, nativeValue.size);
}
}
private static resetFontInternalProperty(view: view.View, nativeValue: any) {
var tv: android.widget.TextView = <android.widget.TextView>view._nativeView;
if (tv && nativeValue) {
tv.setTypeface(nativeValue.typeface);
tv.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, nativeValue.size);
}
}
private static getNativeFontInternalValue(view: view.View): any {
var tv: android.widget.TextView = <android.widget.TextView>view._nativeView;
return {
typeface: tv.getTypeface(),
size: tv.getTextSize()
};
}
// text-align
private static setTextAlignmentProperty(view: view.View, newValue: any) {
var verticalGravity = view._nativeView.getGravity() & android.view.Gravity.VERTICAL_GRAVITY_MASK;
switch (newValue) {
case enums.TextAlignment.left:
view._nativeView.setGravity(android.view.Gravity.LEFT | verticalGravity);
break;
case enums.TextAlignment.center:
view._nativeView.setGravity(android.view.Gravity.CENTER_HORIZONTAL | verticalGravity);
break;
case enums.TextAlignment.right:
view._nativeView.setGravity(android.view.Gravity.RIGHT | verticalGravity);
break;
default:
break;
}
}
private static resetTextAlignmentProperty(view: view.View, nativeValue: any) {
view._nativeView.setGravity(nativeValue);
}
private static getNativeTextAlignmentValue(view: view.View): any {
return view._nativeView.getGravity();
}
// text-decoration
private static setTextDecorationProperty(view: view.View, newValue: any) {
utils.ad.setTextDecoration(view._nativeView, newValue);
}
private static resetTextDecorationProperty(view: view.View, nativeValue: any) {
utils.ad.setTextDecoration(view._nativeView, enums.TextDecoration.none);
}
// text-transform
private static setTextTransformProperty(view: view.View, newValue: any) {
utils.ad.setTextTransform(view, newValue);
}
private static resetTextTransformProperty(view: view.View, nativeValue: any) {
utils.ad.setTextTransform(view, enums.TextTransform.none);
}
// white-space
private static setWhiteSpaceProperty(view: view.View, newValue: any) {
utils.ad.setWhiteSpace(view._nativeView, newValue);
}
private static resetWhiteSpaceProperty(view: view.View, nativeValue: any) {
utils.ad.setWhiteSpace(view._nativeView, enums.WhiteSpace.normal);
}
public static registerHandlers() {
style.registerHandler(style.colorProperty, new style.StylePropertyChangedHandler(
TextBaseStyler.setColorProperty,
TextBaseStyler.resetColorProperty,
TextBaseStyler.getNativeColorValue), "TextBase");
style.registerHandler(style.fontInternalProperty, new style.StylePropertyChangedHandler(
TextBaseStyler.setFontInternalProperty,
TextBaseStyler.resetFontInternalProperty,
TextBaseStyler.getNativeFontInternalValue), "TextBase");
style.registerHandler(style.textAlignmentProperty, new style.StylePropertyChangedHandler(
TextBaseStyler.setTextAlignmentProperty,
TextBaseStyler.resetTextAlignmentProperty,
TextBaseStyler.getNativeTextAlignmentValue), "TextBase");
style.registerHandler(style.textDecorationProperty, new style.StylePropertyChangedHandler(
TextBaseStyler.setTextDecorationProperty,
TextBaseStyler.resetTextDecorationProperty), "TextBase");
style.registerHandler(style.textTransformProperty, new style.StylePropertyChangedHandler(
TextBaseStyler.setTextTransformProperty,
TextBaseStyler.resetTextTransformProperty), "TextBase");
style.registerHandler(style.whiteSpaceProperty, new style.StylePropertyChangedHandler(
TextBaseStyler.setWhiteSpaceProperty,
TextBaseStyler.resetWhiteSpaceProperty), "TextBase");
// Register the same stylers for Button.
// It also derives from TextView but is not under TextBase in our View hierarchy.
style.registerHandler(style.colorProperty, new style.StylePropertyChangedHandler(
TextBaseStyler.setColorProperty,
TextBaseStyler.resetColorProperty,
TextBaseStyler.getNativeColorValue), "Button");
style.registerHandler(style.fontInternalProperty, new style.StylePropertyChangedHandler(
TextBaseStyler.setFontInternalProperty,
TextBaseStyler.resetFontInternalProperty,
TextBaseStyler.getNativeFontInternalValue), "Button");
style.registerHandler(style.textAlignmentProperty, new style.StylePropertyChangedHandler(
TextBaseStyler.setTextAlignmentProperty,
TextBaseStyler.resetTextAlignmentProperty,
TextBaseStyler.getNativeTextAlignmentValue), "Button");
style.registerHandler(style.textDecorationProperty, new style.StylePropertyChangedHandler(
TextBaseStyler.setTextDecorationProperty,
TextBaseStyler.resetTextDecorationProperty), "Button");
style.registerHandler(style.textTransformProperty, new style.StylePropertyChangedHandler(
TextBaseStyler.setTextTransformProperty,
TextBaseStyler.resetTextTransformProperty), "Button");
style.registerHandler(style.whiteSpaceProperty, new style.StylePropertyChangedHandler(
TextBaseStyler.setWhiteSpaceProperty,
TextBaseStyler.resetWhiteSpaceProperty), "Button");
}
}