UNPKG

14.1 kBJavaScriptView Raw
1var TextView_1;
2import { textProperty } from '../text-base';
3import { TextViewBase as TextViewBaseCommon } from './text-view-common';
4import { editableProperty, hintProperty, placeholderColorProperty, _updateCharactersInRangeReplacementString } from '../editable-text-base';
5import { CSSType } from '../core/view';
6import { colorProperty, borderTopWidthProperty, borderRightWidthProperty, borderBottomWidthProperty, borderLeftWidthProperty, paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty } from '../styling/style-properties';
7import { iOSNativeHelper, layout } from '../../utils';
8const majorVersion = iOSNativeHelper.MajorVersion;
9var UITextViewDelegateImpl = /** @class */ (function (_super) {
10 __extends(UITextViewDelegateImpl, _super);
11 function UITextViewDelegateImpl() {
12 return _super !== null && _super.apply(this, arguments) || this;
13 }
14 UITextViewDelegateImpl.initWithOwner = function (owner) {
15 var impl = UITextViewDelegateImpl.new();
16 impl._owner = owner;
17 return impl;
18 };
19 UITextViewDelegateImpl.prototype.textViewShouldBeginEditing = function (textView) {
20 var _a;
21 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
22 if (owner) {
23 return owner.textViewShouldBeginEditing(textView);
24 }
25 return true;
26 };
27 UITextViewDelegateImpl.prototype.textViewDidBeginEditing = function (textView) {
28 var _a;
29 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
30 if (owner) {
31 owner.textViewDidBeginEditing(textView);
32 }
33 };
34 UITextViewDelegateImpl.prototype.textViewDidEndEditing = function (textView) {
35 var _a;
36 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
37 if (owner) {
38 owner.textViewDidEndEditing(textView);
39 }
40 };
41 UITextViewDelegateImpl.prototype.textViewDidChange = function (textView) {
42 var _a;
43 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
44 if (owner) {
45 owner.textViewDidChange(textView);
46 }
47 };
48 UITextViewDelegateImpl.prototype.textViewShouldChangeTextInRangeReplacementText = function (textView, range, replacementString) {
49 var _a;
50 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
51 if (owner) {
52 return owner.textViewShouldChangeTextInRangeReplacementText(textView, range, replacementString);
53 }
54 return true;
55 };
56 UITextViewDelegateImpl.prototype.scrollViewDidScroll = function (sv) {
57 var _a;
58 var owner = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.deref();
59 if (owner) {
60 return owner.scrollViewDidScroll(sv);
61 }
62 };
63 UITextViewDelegateImpl.ObjCProtocols = [UITextViewDelegate];
64 return UITextViewDelegateImpl;
65}(NSObject));
66var NoScrollAnimationUITextView = /** @class */ (function (_super) {
67 __extends(NoScrollAnimationUITextView, _super);
68 function NoScrollAnimationUITextView() {
69 return _super !== null && _super.apply(this, arguments) || this;
70 }
71 // see https://github.com/NativeScript/NativeScript/issues/6863
72 // UITextView internally scrolls the text you are currently typing to visible when newline character
73 // is typed but the scroll animation is not needed because at the same time we are expanding
74 // the textview (setting its frame)
75 NoScrollAnimationUITextView.prototype.setContentOffsetAnimated = function (contentOffset, animated) {
76 _super.prototype.setContentOffsetAnimated.call(this, contentOffset, false);
77 };
78 return NoScrollAnimationUITextView;
79}(UITextView));
80let TextView = TextView_1 = class TextView extends TextViewBaseCommon {
81 constructor() {
82 super(...arguments);
83 this._hintColor = majorVersion <= 12 || !UIColor.placeholderTextColor ? UIColor.blackColor.colorWithAlphaComponent(0.22) : UIColor.placeholderTextColor;
84 this._textColor = majorVersion <= 12 || !UIColor.labelColor ? null : UIColor.labelColor;
85 }
86 createNativeView() {
87 const textView = NoScrollAnimationUITextView.new();
88 if (!textView.font) {
89 textView.font = UIFont.systemFontOfSize(12);
90 }
91 return textView;
92 }
93 initNativeView() {
94 super.initNativeView();
95 this._delegate = UITextViewDelegateImpl.initWithOwner(new WeakRef(this));
96 this.nativeTextViewProtected.delegate = this._delegate;
97 }
98 disposeNativeView() {
99 this._delegate = null;
100 super.disposeNativeView();
101 }
102 // @ts-ignore
103 get ios() {
104 return this.nativeViewProtected;
105 }
106 textViewShouldBeginEditing(textView) {
107 if (this._isShowingHint) {
108 this.showText();
109 }
110 return this.editable;
111 }
112 textViewDidBeginEditing(textView) {
113 this._isEditing = true;
114 this.notify({ eventName: TextView_1.focusEvent, object: this });
115 }
116 textViewDidEndEditing(textView) {
117 if (this.updateTextTrigger === 'focusLost') {
118 textProperty.nativeValueChange(this, textView.text);
119 }
120 this._isEditing = false;
121 this.dismissSoftInput();
122 this._refreshHintState(this.hint, textView.text);
123 }
124 textViewDidChange(textView) {
125 if (this.updateTextTrigger === 'textChanged') {
126 textProperty.nativeValueChange(this, textView.text);
127 }
128 this.requestLayout();
129 }
130 textViewShouldChangeTextInRangeReplacementText(textView, range, replacementString) {
131 const delta = replacementString.length - range.length;
132 if (delta > 0) {
133 if (textView.text.length + delta > this.maxLength) {
134 return false;
135 }
136 }
137 if (replacementString === '\n') {
138 this.notify({ eventName: TextView_1.returnPressEvent, object: this });
139 }
140 if (this.formattedText) {
141 _updateCharactersInRangeReplacementString(this.formattedText, range.location, range.length, replacementString);
142 }
143 return true;
144 }
145 scrollViewDidScroll(sv) {
146 const contentOffset = this.nativeViewProtected.contentOffset;
147 this.notify({
148 object: this,
149 eventName: 'scroll',
150 scrollX: contentOffset.x,
151 scrollY: contentOffset.y,
152 });
153 }
154 _refreshHintState(hint, text) {
155 if (this.formattedText) {
156 return;
157 }
158 if (text !== null && text !== undefined && text !== '') {
159 this.showText();
160 }
161 else if (!this._isEditing && hint !== null && hint !== undefined && hint !== '') {
162 this.showHint(hint);
163 }
164 else {
165 this._isShowingHint = false;
166 this.nativeTextViewProtected.text = '';
167 }
168 }
169 _refreshColor() {
170 if (this._isShowingHint) {
171 const placeholderColor = this.style.placeholderColor;
172 const color = this.style.color;
173 if (placeholderColor) {
174 this.nativeTextViewProtected.textColor = placeholderColor.ios;
175 }
176 else if (color) {
177 // Use semi-transparent version of color for back-compatibility
178 this.nativeTextViewProtected.textColor = color.ios.colorWithAlphaComponent(0.22);
179 }
180 else {
181 this.nativeTextViewProtected.textColor = this._hintColor;
182 }
183 }
184 else {
185 const color = this.style.color;
186 if (color) {
187 this.nativeTextViewProtected.textColor = color.ios;
188 this.nativeTextViewProtected.tintColor = color.ios;
189 }
190 else {
191 this.nativeTextViewProtected.textColor = this._textColor;
192 this.nativeTextViewProtected.tintColor = this._textColor;
193 }
194 }
195 }
196 showHint(hint) {
197 const nativeView = this.nativeTextViewProtected;
198 this._isShowingHint = true;
199 this._refreshColor();
200 const hintAsString = hint === null || hint === undefined ? '' : hint.toString();
201 nativeView.text = hintAsString;
202 }
203 showText() {
204 this._isShowingHint = false;
205 this._setNativeText();
206 this._refreshColor();
207 this.requestLayout();
208 }
209 [textProperty.getDefault]() {
210 return '';
211 }
212 [textProperty.setNative](value) {
213 this._refreshHintState(this.hint, value);
214 }
215 [hintProperty.getDefault]() {
216 return '';
217 }
218 [hintProperty.setNative](value) {
219 this._refreshHintState(value, this.text);
220 }
221 [editableProperty.getDefault]() {
222 return this.nativeTextViewProtected.editable;
223 }
224 [editableProperty.setNative](value) {
225 this.nativeTextViewProtected.editable = value;
226 }
227 [colorProperty.setNative](color) {
228 this._refreshColor();
229 }
230 [placeholderColorProperty.setNative](value) {
231 this._refreshColor();
232 }
233 [borderTopWidthProperty.getDefault]() {
234 return {
235 value: this.nativeTextViewProtected.textContainerInset.top,
236 unit: 'px',
237 };
238 }
239 [borderTopWidthProperty.setNative](value) {
240 const inset = this.nativeTextViewProtected.textContainerInset;
241 const top = layout.toDeviceIndependentPixels(this.effectivePaddingTop + this.effectiveBorderTopWidth);
242 this.nativeTextViewProtected.textContainerInset = {
243 top: top,
244 left: inset.left,
245 bottom: inset.bottom,
246 right: inset.right,
247 };
248 }
249 [borderRightWidthProperty.getDefault]() {
250 return {
251 value: this.nativeTextViewProtected.textContainerInset.right,
252 unit: 'px',
253 };
254 }
255 [borderRightWidthProperty.setNative](value) {
256 const inset = this.nativeTextViewProtected.textContainerInset;
257 const right = layout.toDeviceIndependentPixels(this.effectivePaddingRight + this.effectiveBorderRightWidth);
258 this.nativeTextViewProtected.textContainerInset = {
259 top: inset.top,
260 left: inset.left,
261 bottom: inset.bottom,
262 right: right,
263 };
264 }
265 [borderBottomWidthProperty.getDefault]() {
266 return {
267 value: this.nativeTextViewProtected.textContainerInset.bottom,
268 unit: 'px',
269 };
270 }
271 [borderBottomWidthProperty.setNative](value) {
272 const inset = this.nativeTextViewProtected.textContainerInset;
273 const bottom = layout.toDeviceIndependentPixels(this.effectivePaddingBottom + this.effectiveBorderBottomWidth);
274 this.nativeTextViewProtected.textContainerInset = {
275 top: inset.top,
276 left: inset.left,
277 bottom: bottom,
278 right: inset.right,
279 };
280 }
281 [borderLeftWidthProperty.getDefault]() {
282 return {
283 value: this.nativeTextViewProtected.textContainerInset.left,
284 unit: 'px',
285 };
286 }
287 [borderLeftWidthProperty.setNative](value) {
288 const inset = this.nativeTextViewProtected.textContainerInset;
289 const left = layout.toDeviceIndependentPixels(this.effectivePaddingLeft + this.effectiveBorderLeftWidth);
290 this.nativeTextViewProtected.textContainerInset = {
291 top: inset.top,
292 left: left,
293 bottom: inset.bottom,
294 right: inset.right,
295 };
296 }
297 [paddingTopProperty.getDefault]() {
298 return {
299 value: this.nativeTextViewProtected.textContainerInset.top,
300 unit: 'px',
301 };
302 }
303 [paddingTopProperty.setNative](value) {
304 const inset = this.nativeTextViewProtected.textContainerInset;
305 const top = layout.toDeviceIndependentPixels(this.effectivePaddingTop + this.effectiveBorderTopWidth);
306 this.nativeTextViewProtected.textContainerInset = {
307 top: top,
308 left: inset.left,
309 bottom: inset.bottom,
310 right: inset.right,
311 };
312 }
313 [paddingRightProperty.getDefault]() {
314 return {
315 value: this.nativeTextViewProtected.textContainerInset.right,
316 unit: 'px',
317 };
318 }
319 [paddingRightProperty.setNative](value) {
320 const inset = this.nativeTextViewProtected.textContainerInset;
321 const right = layout.toDeviceIndependentPixels(this.effectivePaddingRight + this.effectiveBorderRightWidth);
322 this.nativeTextViewProtected.textContainerInset = {
323 top: inset.top,
324 left: inset.left,
325 bottom: inset.bottom,
326 right: right,
327 };
328 }
329 [paddingBottomProperty.getDefault]() {
330 return {
331 value: this.nativeTextViewProtected.textContainerInset.bottom,
332 unit: 'px',
333 };
334 }
335 [paddingBottomProperty.setNative](value) {
336 const inset = this.nativeTextViewProtected.textContainerInset;
337 const bottom = layout.toDeviceIndependentPixels(this.effectivePaddingBottom + this.effectiveBorderBottomWidth);
338 this.nativeTextViewProtected.textContainerInset = {
339 top: inset.top,
340 left: inset.left,
341 bottom: bottom,
342 right: inset.right,
343 };
344 }
345 [paddingLeftProperty.getDefault]() {
346 return {
347 value: this.nativeTextViewProtected.textContainerInset.left,
348 unit: 'px',
349 };
350 }
351 [paddingLeftProperty.setNative](value) {
352 const inset = this.nativeTextViewProtected.textContainerInset;
353 const left = layout.toDeviceIndependentPixels(this.effectivePaddingLeft + this.effectiveBorderLeftWidth);
354 this.nativeTextViewProtected.textContainerInset = {
355 top: inset.top,
356 left: left,
357 bottom: inset.bottom,
358 right: inset.right,
359 };
360 }
361};
362TextView = TextView_1 = __decorate([
363 CSSType('TextView')
364], TextView);
365export { TextView };
366TextView.prototype.recycleNativeView = 'auto';
367//# sourceMappingURL=index.ios.js.map
\No newline at end of file