Repository navigation
Expand file tree
/
Copy pathbutton.ios.ts
More file actions
232 lines (192 loc) · 9.45 KB
/
Copy pathbutton.ios.ts
File metadata and controls
232 lines (192 loc) · 9.45 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import common = require("./button-common");
import stateChanged = require("ui/core/control-state-change");
import style = require("ui/styling/style");
import font = require("ui/styling/font");
import view = require("ui/core/view");
import utils = require("utils/utils");
import enums = require("ui/enums");
import dependencyObservable = require("ui/core/dependency-observable");
import styleScope = require("../styling/style-scope");
class TapHandlerImpl extends NSObject {
private _owner: WeakRef<Button>;
public static initWithOwner(owner: WeakRef<Button>): TapHandlerImpl {
let handler = <TapHandlerImpl>TapHandlerImpl.new();
handler._owner = owner;
return handler;
}
public tap(args) {
let owner = this._owner.get();
if (owner) {
owner._emit(common.Button.tapEvent);
}
}
public static ObjCExposedMethods = {
"tap": { returns: interop.types.void, params: [interop.types.id] }
};
}
global.moduleMerge(common, exports);
export class Button extends common.Button {
private _ios: UIButton;
private _tapHandler: NSObject;
private _stateChangedHandler: stateChanged.ControlStateChangeListener;
constructor() {
super();
this._ios = UIButton.buttonWithType(UIButtonType.UIButtonTypeSystem);
this._tapHandler = TapHandlerImpl.initWithOwner(new WeakRef(this));
this._ios.addTargetActionForControlEvents(this._tapHandler, "tap", UIControlEvents.UIControlEventTouchUpInside);
this._stateChangedHandler = new stateChanged.ControlStateChangeListener(this._ios, (s: string) => {
this._goToVisualState(s);
});
}
public onLoaded() {
super.onLoaded();
if (this.parent !== null && this.page !== null) {
let rootPage = this.page;
let scope: styleScope.StyleScope = (<any>rootPage)._getStyleScope();
if (scope.getVisualStates(this) !== undefined) {
this._stateChangedHandler.start();
}
}
}
public onUnloaded() {
super.onUnloaded();
this._stateChangedHandler.stop();
}
get ios(): UIButton {
return this._ios;
}
public _onTextPropertyChanged(data: dependencyObservable.PropertyChangeData) {
// In general, if a property is not specified for a state, the default is to use
// the UIControlStateNormal value. If the value for UIControlStateNormal is not set,
// then the property defaults to a system value. Therefore, at a minimum, you should
// set the value for the normal state.
this.ios.setTitleForState(data.newValue + "", UIControlState.UIControlStateNormal);
}
public _setFormattedTextPropertyToNative(value) {
// In general, if a property is not specified for a state, the default is to use
// the UIControlStateNormal value. If the value for UIControlStateNormal is not set,
// then the property defaults to a system value. Therefore, at a minimum, you should
// set the value for the normal state.
var newText = value ? value._formattedText : null;
this.ios.setAttributedTitleForState(newText, UIControlState.UIControlStateNormal);
this.style._updateTextDecoration();
}
}
export class ButtonStyler implements style.Styler {
// color
private static setColorProperty(view: view.View, newValue: any) {
var btn: UIButton = <UIButton>view._nativeView;
btn.setTitleColorForState(newValue, UIControlState.UIControlStateNormal);
}
private static resetColorProperty(view: view.View, nativeValue: any) {
var btn: UIButton = <UIButton>view._nativeView;
btn.setTitleColorForState(nativeValue, UIControlState.UIControlStateNormal);
}
private static getNativeColorValue(view: view.View): any {
var btn: UIButton = <UIButton>view._nativeView;
return btn.titleColorForState(UIControlState.UIControlStateNormal);
}
// font
private static setFontInternalProperty(view: view.View, newValue: any, nativeValue?: any) {
var btn: UIButton = <UIButton>view._nativeView;
btn.titleLabel.font = (<font.Font>newValue).getUIFont(nativeValue);
}
private static resetFontInternalProperty(view: view.View, nativeValue: any) {
var btn: UIButton = <UIButton>view._nativeView;
btn.titleLabel.font = nativeValue;
}
private static getNativeFontInternalValue(view: view.View): any {
var btn: UIButton = <UIButton>view._nativeView;
return btn.titleLabel.font;
}
// text-align
private static setTextAlignmentProperty(view: view.View, newValue: any) {
var btn: UIButton = <UIButton>view._nativeView;
utils.ios.setTextAlignment(btn.titleLabel, newValue);
// Also set the contentHorizontalAlignment
switch (newValue) {
case enums.TextAlignment.left:
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.UIControlContentHorizontalAlignmentLeft;
break;
case enums.TextAlignment.center:
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.UIControlContentHorizontalAlignmentCenter;
break;
case enums.TextAlignment.right:
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.UIControlContentHorizontalAlignmentRight;
break;
default:
break;
}
}
private static resetTextAlignmentProperty(view: view.View, nativeValue: any) {
var btn: UIButton = <UIButton>view._nativeView;
btn.titleLabel.textAlignment = nativeValue.textAlign;
btn.contentHorizontalAlignment = nativeValue.contentAlign;
}
private static getNativeTextAlignmentValue(view: view.View): any {
var btn: UIButton = <UIButton>view._nativeView;
return {
textAlign: btn.titleLabel.textAlignment,
contentAlign: btn.contentHorizontalAlignment
}
}
// Padding
private static setPaddingProperty(view: view.View, newValue: any) {
var top = newValue.top + view.borderWidth;
var left = newValue.left + view.borderWidth;
var bottom = newValue.bottom + view.borderWidth;
var right = newValue.right + view.borderWidth;
(<UIButton>view._nativeView).contentEdgeInsets = UIEdgeInsetsFromString(`{${top},${left},${bottom},${right}}`);
}
private static resetPaddingProperty(view: view.View, nativeValue: any) {
(<UIButton>view._nativeView).contentEdgeInsets = UIEdgeInsetsFromString("{0,0,0,0}");
}
// text-decoration
private static setTextDecorationProperty(view: view.View, newValue: any) {
utils.ios.setTextDecorationAndTransform(view, newValue, view.style.textTransform, view.style.letterSpacing);
}
private static resetTextDecorationProperty(view: view.View, nativeValue: any) {
utils.ios.setTextDecorationAndTransform(view, enums.TextDecoration.none, view.style.textTransform, view.style.letterSpacing);
}
// text-transform
private static setTextTransformProperty(view: view.View, newValue: any) {
utils.ios.setTextDecorationAndTransform(view, view.style.textDecoration, newValue, view.style.letterSpacing);
}
private static resetTextTransformProperty(view: view.View, nativeValue: any) {
utils.ios.setTextDecorationAndTransform(view, view.style.textDecoration, enums.TextTransform.none, view.style.letterSpacing);
}
// white-space
private static setWhiteSpaceProperty(view: view.View, newValue: any) {
utils.ios.setWhiteSpace((<UIButton>view.ios).titleLabel, newValue, view.ios);
}
private static resetWhiteSpaceProperty(view: view.View, nativeValue: any) {
utils.ios.setWhiteSpace((<UIButton>view.ios).titleLabel, enums.WhiteSpace.normal, view.ios);
}
public static registerHandlers() {
style.registerHandler(style.colorProperty, new style.StylePropertyChangedHandler(
ButtonStyler.setColorProperty,
ButtonStyler.resetColorProperty,
ButtonStyler.getNativeColorValue), "Button");
style.registerHandler(style.fontInternalProperty, new style.StylePropertyChangedHandler(
ButtonStyler.setFontInternalProperty,
ButtonStyler.resetFontInternalProperty,
ButtonStyler.getNativeFontInternalValue), "Button");
style.registerHandler(style.textAlignmentProperty, new style.StylePropertyChangedHandler(
ButtonStyler.setTextAlignmentProperty,
ButtonStyler.resetTextAlignmentProperty,
ButtonStyler.getNativeTextAlignmentValue), "Button");
style.registerHandler(style.nativePaddingsProperty, new style.StylePropertyChangedHandler(
ButtonStyler.setPaddingProperty,
ButtonStyler.resetPaddingProperty), "Button");
style.registerHandler(style.textDecorationProperty, new style.StylePropertyChangedHandler(
ButtonStyler.setTextDecorationProperty,
ButtonStyler.resetTextDecorationProperty), "Button");
style.registerHandler(style.textTransformProperty, new style.StylePropertyChangedHandler(
ButtonStyler.setTextTransformProperty,
ButtonStyler.resetTextTransformProperty), "Button");
style.registerHandler(style.whiteSpaceProperty, new style.StylePropertyChangedHandler(
ButtonStyler.setWhiteSpaceProperty,
ButtonStyler.resetWhiteSpaceProperty), "Button");
}
}
ButtonStyler.registerHandlers();