forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-view.ios.ts
More file actions
173 lines (143 loc) · 5.39 KB
/
text-view.ios.ts
File metadata and controls
173 lines (143 loc) · 5.39 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
import common = require("./text-view-common");
import {PropertyChangeData} from "ui/core/dependency-observable";
import {TextBase} from "ui/text-base";
import {UpdateTextTrigger} from "ui/enums";
import {View} from "ui/core/view";
import * as style from "ui/styling/style";
import {isNullOrUndefined} from "utils/types";
global.moduleMerge(common, exports);
class UITextViewDelegateImpl extends NSObject implements UITextViewDelegate {
public static ObjCProtocols = [UITextViewDelegate];
private _owner: WeakRef<TextView>;
public static initWithOwner(owner: WeakRef<TextView>): UITextViewDelegateImpl {
let impl = <UITextViewDelegateImpl>UITextViewDelegateImpl.new();
impl._owner = owner;
return impl;
}
public textViewShouldBeginEditing(textView: UITextView): boolean {
let owner = this._owner.get();
if (owner) {
owner._hideHint();
}
return true;
}
public textViewDidBeginEditing(textView: UITextView) {
let owner = this._owner.get();
if (owner) {
owner.style._updateTextDecoration();
owner.style._updateTextTransform();
}
}
public textViewDidEndEditing(textView: UITextView) {
let owner = this._owner.get();
if (owner) {
if (owner.updateTextTrigger === UpdateTextTrigger.focusLost) {
owner._onPropertyChangedFromNative(TextBase.textProperty, textView.text);
}
owner.dismissSoftInput();
owner._refreshHintState(owner.hint, textView.text);
owner.style._updateTextDecoration();
owner.style._updateTextTransform();
}
}
public textViewDidChange(textView: UITextView) {
let owner = this._owner.get();
if (owner) {
var range = textView.selectedRange;
owner.style._updateTextDecoration();
owner.style._updateTextTransform();
textView.selectedRange = range;
if (owner.updateTextTrigger === UpdateTextTrigger.textChanged) {
owner._onPropertyChangedFromNative(TextBase.textProperty, textView.text);
}
}
}
}
export class TextView extends common.TextView {
private _ios: UITextView;
private _delegate: UITextViewDelegateImpl;
constructor() {
super();
this._ios = new UITextView();
if (!this._ios.font) {
this._ios.font = UIFont.systemFontOfSize(12);
}
this._delegate = UITextViewDelegateImpl.initWithOwner(new WeakRef(this));
}
public onLoaded() {
super.onLoaded();
this._ios.delegate = this._delegate;
}
public onUnloaded() {
this._ios.delegate = null;
super.onUnloaded();
}
get ios(): UITextView {
return this._ios;
}
public _onEditablePropertyChanged(data: PropertyChangeData) {
this._ios.editable = data.newValue;
}
public _onHintPropertyChanged(data: PropertyChangeData) {
this._refreshHintState(data.newValue, this.text);
}
public _onTextPropertyChanged(data: PropertyChangeData) {
super._onTextPropertyChanged(data);
this._refreshHintState(this.hint, data.newValue);
}
public _refreshHintState(hint: string, text: string) {
if (hint && !text) {
this._showHint(hint);
}
else {
this._hideHint();
}
}
public _showHint(hint: string) {
this.ios.textColor = this.ios.textColor ? this.ios.textColor.colorWithAlphaComponent(0.22) : UIColor.blackColor().colorWithAlphaComponent(0.22);
this.ios.text = isNullOrUndefined(hint) ? "" : hint + "";
(<any>this.ios).isShowingHint = true;
}
public _hideHint() {
this.ios.textColor = this.color ? this.color.ios : null;
this.ios.text = isNullOrUndefined(this.text) ? "" : this.text + "";
(<any>this.ios).isShowingHint = false;
}
}
export class TextViewStyler implements style.Styler {
// Color methods
private static setColorProperty(v: View, newValue: any) {
var textView: UITextView = <UITextView>v._nativeView;
TextViewStyler._setTextViewColor(textView, newValue);
}
private static resetColorProperty(v: View, nativeValue: any) {
var textView: UITextView = <UITextView>v._nativeView;
TextViewStyler._setTextViewColor(textView, nativeValue);
}
private static _setTextViewColor(textView: UITextView, newValue: any) {
var color: UIColor = <UIColor>newValue;
if ((<any>textView).isShowingHint && color) {
textView.textColor = (<UIColor>color).colorWithAlphaComponent(0.22);
}
else {
textView.textColor = color;
textView.tintColor = color;
}
}
private static getNativeColorValue(v: View): any {
var textView: UITextView = <UITextView>v._nativeView;
if ((<any>textView).isShowingHint && textView.textColor) {
return textView.textColor.colorWithAlphaComponent(1);
}
else {
return textView.textColor;
}
}
public static registerHandlers() {
style.registerHandler(style.colorProperty, new style.StylePropertyChangedHandler(
TextViewStyler.setColorProperty,
TextViewStyler.resetColorProperty,
TextViewStyler.getNativeColorValue), "TextView");
}
}
TextViewStyler.registerHandlers();