forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 126
/
RCTUITextField.m
549 lines (447 loc) · 16.7 KB
/
RCTUITextField.m
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <React/RCTUITextField.h>
#import <React/RCTUtils.h>
#import <React/UIView+React.h>
#import <React/RCTBackedTextInputDelegateAdapter.h>
#import <React/RCTBackedTextInputDelegate.h> // TODO(OSS Candidate ISS#2710739)
#import <React/RCTTextAttributes.h>
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
#if RCT_SUBCLASS_SECURETEXTFIELD
#define RCTUITextFieldCell RCTUISecureTextFieldCell
@interface RCTUISecureTextFieldCell : NSSecureTextFieldCell
#else
@interface RCTUITextFieldCell : NSTextFieldCell
#endif
@property (nonatomic, assign) UIEdgeInsets textContainerInset;
@property (nonatomic, getter=isAutomaticTextReplacementEnabled) BOOL automaticTextReplacementEnabled;
@property (nonatomic, getter=isAutomaticSpellingCorrectionEnabled) BOOL automaticSpellingCorrectionEnabled;
@property (nonatomic, strong, nullable) RCTUIColor *selectionColor;
@end
@implementation RCTUITextFieldCell
- (void)setTextContainerInset:(UIEdgeInsets)textContainerInset
{
_textContainerInset = textContainerInset;
}
- (NSRect)titleRectForBounds:(NSRect)rect
{
return UIEdgeInsetsInsetRect([super titleRectForBounds:rect], self.textContainerInset);
}
- (void)editWithFrame:(NSRect)rect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)delegate event:(NSEvent *)event
{
[super editWithFrame:[self titleRectForBounds:rect] inView:controlView editor:textObj delegate:delegate event:event];
}
- (void)selectWithFrame:(NSRect)rect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)delegate start:(NSInteger)selStart length:(NSInteger)selLength
{
[super selectWithFrame:[self titleRectForBounds:rect] inView:controlView editor:textObj delegate:delegate start:selStart length:selLength];
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
if (self.drawsBackground) {
if (self.backgroundColor && self.backgroundColor.alphaComponent > 0) {
[self.backgroundColor set];
NSRectFill(cellFrame);
}
}
[super drawInteriorWithFrame:[self titleRectForBounds:cellFrame] inView:controlView];
}
- (NSText *)setUpFieldEditorAttributes:(NSText *)textObj
{
NSTextView *fieldEditor = (NSTextView *)[super setUpFieldEditorAttributes:textObj];
fieldEditor.automaticSpellingCorrectionEnabled = self.isAutomaticSpellingCorrectionEnabled;
fieldEditor.automaticTextReplacementEnabled = self.isAutomaticTextReplacementEnabled;
NSMutableDictionary *selectTextAttributes = fieldEditor.selectedTextAttributes.mutableCopy;
selectTextAttributes[NSBackgroundColorAttributeName] = self.selectionColor ?: [NSColor selectedControlColor];
fieldEditor.selectedTextAttributes = selectTextAttributes;
fieldEditor.insertionPointColor = self.selectionColor ?: [RCTUIColor selectedControlColor];
return fieldEditor;
}
@end
#endif // ]TODO(macOS ISS#2323203)
#ifdef RCT_SUBCLASS_SECURETEXTFIELD
@implementation RCTUISecureTextField {
#else
@implementation RCTUITextField {
#endif
RCTBackedTextFieldDelegateAdapter *_textInputDelegateAdapter;
NSDictionary<NSAttributedStringKey, id> *_defaultTextAttributes;
}
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
@dynamic delegate;
static RCTUIColor *defaultPlaceholderTextColor()
{
return [NSColor placeholderTextColor];
}
#endif // ]TODO(macOS ISS#2323203)
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_textDidChange)
name:UITextFieldTextDidChangeNotification
object:self];
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
[self setBordered:NO];
[self setAllowsEditingTextAttributes:YES];
[self setAccessibilityRole:NSAccessibilityTextFieldRole];
[self setBackgroundColor:[NSColor clearColor]];
#endif // ]TODO(macOS ISS#2323203)
_textInputDelegateAdapter = [[RCTBackedTextFieldDelegateAdapter alloc] initWithTextField:self];
}
return self;
}
- (void)_textDidChange
{
_textWasPasted = NO;
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
[self setAttributedText:[[NSAttributedString alloc] initWithString:[self text]
attributes:[self defaultTextAttributes]]];
#endif // ]TODO(macOS ISS#2323203)
}
#pragma mark - Accessibility
- (void)setIsAccessibilityElement:(BOOL)isAccessibilityElement
{
// UITextField is accessible by default (some nested views are) and disabling that is not supported.
// On iOS accessible elements cannot be nested, therefore enabling accessibility for some container view
// (even in a case where this view is a part of public API of TextInput on iOS) shadows some features implemented inside the component.
}
#pragma mark - Properties
- (void)setTextContainerInset:(UIEdgeInsets)textContainerInset
{
_textContainerInset = textContainerInset;
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
[self setNeedsLayout];
#else // [TODO(macOS ISS#2323203)
((RCTUITextFieldCell*)self.cell).textContainerInset = _textContainerInset;
#endif // ]TODO(macOS ISS#2323203)
}
#if TARGET_OS_OSX // TODO(macOS ISS#2323203)
+ (Class)cellClass
{
return RCTUITextFieldCell.class;
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
self.attributedStringValue = attributedText;
}
- (NSAttributedString *)attributedText
{
return self.attributedStringValue;
}
- (void)setText:(NSString *)text
{
self.stringValue = text;
}
- (NSString*)text
{
return self.stringValue;
}
- (void)setAutomaticTextReplacementEnabled:(BOOL)automaticTextReplacementEnabled
{
((RCTUITextFieldCell*)self.cell).automaticTextReplacementEnabled = automaticTextReplacementEnabled;
}
- (BOOL)isAutomaticTextReplacementEnabled
{
return ((RCTUITextFieldCell*)self.cell).isAutomaticTextReplacementEnabled;
}
- (void)setAutomaticSpellingCorrectionEnabled:(BOOL)automaticSpellingCorrectionEnabled
{
((RCTUITextFieldCell*)self.cell).automaticSpellingCorrectionEnabled = automaticSpellingCorrectionEnabled;
}
- (BOOL)isAutomaticSpellingCorrectionEnabled
{
return ((RCTUITextFieldCell*)self.cell).isAutomaticSpellingCorrectionEnabled;
}
- (void)setSelectionColor:(RCTUIColor *)selectionColor // TODO(OSS Candidate ISS#2710739)
{
((RCTUITextFieldCell*)self.cell).selectionColor = selectionColor;
}
- (RCTUIColor*)selectionColor // TODO(OSS Candidate ISS#2710739)
{
return ((RCTUITextFieldCell*)self.cell).selectionColor;
}
- (void)setFont:(UIFont *)font
{
((RCTUITextFieldCell*)self.cell).font = font;
}
- (UIFont *)font
{
return ((RCTUITextFieldCell*)self.cell).font;
}
#endif // ]TODO(macOS ISS#2323203)
- (void)setPlaceholder:(NSString *)placeholder
{
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
[super setPlaceholder:placeholder];
#else // [TODO(macOS ISS#2323203)
[super setPlaceholderString:placeholder];
#endif // ]TODO(macOS ISS#2323203)
[self _updatePlaceholder];
}
- (NSString*)placeholder // [TODO(macOS ISS#2323203)
{
#if !TARGET_OS_OSX
return super.placeholder;
#else
return self.placeholderAttributedString.string ?: self.placeholderString;
#endif
} // ]TODO(macOS ISS#2323203)
- (void)setPlaceholderColor:(RCTUIColor *)placeholderColor // TODO(OSS Candidate ISS#2710739)
{
_placeholderColor = placeholderColor;
[self _updatePlaceholder];
}
- (void)setDefaultTextAttributes:(NSDictionary<NSAttributedStringKey, id> *)defaultTextAttributes
{
_defaultTextAttributes = defaultTextAttributes;
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
[super setDefaultTextAttributes:defaultTextAttributes];
#endif // TODO(macOS ISS#2323203)
[self _updatePlaceholder];
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
[self setAttributedText:[[NSAttributedString alloc] initWithString:[self text]
attributes:[self defaultTextAttributes]]];
#endif // ]TODO(macOS ISS#2323203)
}
- (NSDictionary<NSAttributedStringKey, id> *)defaultTextAttributes
{
return _defaultTextAttributes;
}
- (void)_updatePlaceholder
{
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder ?: @""
attributes:[self _placeholderTextAttributes]];
#else // [TODO(macOS ISS#2323203)
self.placeholderAttributedString = [[NSAttributedString alloc] initWithString:self.placeholder ?: @""
attributes:[self _placeholderTextAttributes]];
#endif // ]TODO(macOS ISS#2323203)
}
- (BOOL)isEditable
{
return self.isEnabled;
}
- (void)setEditable:(BOOL)editable
{
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
// on macos the super must be called otherwise its NSTextFieldCell editable property doesn't get set.
[super setEditable:editable];
#endif // ]TODO(macOS ISS#2323203)
self.enabled = editable;
}
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
- (void)setScrollEnabled:(BOOL)enabled
{
// Do noting, compatible with multiline textinput
}
- (BOOL)scrollEnabled
{
return NO;
}
- (void)setSecureTextEntry:(BOOL)secureTextEntry
{
if (self.secureTextEntry == secureTextEntry) {
return;
}
[super setSecureTextEntry:secureTextEntry];
// Fix for trailing whitespate issue
// Read more:
// https://stackoverflow.com/questions/14220187/uitextfield-has-trailing-whitespace-after-securetextentry-toggle/22537788#22537788
NSAttributedString *originalText = [self.attributedText copy];
self.attributedText = [NSAttributedString new];
self.attributedText = originalText;
}
#endif // ]TODO(macOS ISS#2323203)
#pragma mark - Placeholder
- (NSDictionary<NSAttributedStringKey, id> *)_placeholderTextAttributes
{
NSMutableDictionary<NSAttributedStringKey, id> *textAttributes = [_defaultTextAttributes mutableCopy] ?: [NSMutableDictionary new];
if (self.placeholderColor) {
[textAttributes setValue:self.placeholderColor forKey:NSForegroundColorAttributeName];
} else {
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
[textAttributes setValue:defaultPlaceholderTextColor() forKey:NSForegroundColorAttributeName];
#else
[textAttributes removeObjectForKey:NSForegroundColorAttributeName];
#endif // ]TODO(macOS ISS#2323203)
}
return textAttributes;
}
#pragma mark - Context Menu
#if !TARGET_OS_OSX // [TODO(macOS ISS#2323203)
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (_contextMenuHidden) {
return NO;
}
return [super canPerformAction:action withSender:sender];
}
#pragma mark - Caret Manipulation
- (CGRect)caretRectForPosition:(UITextPosition *)position
{
if (_caretHidden) {
return CGRectZero;
}
return [super caretRectForPosition:position];
}
#pragma mark - Positioning Overrides
- (CGRect)textRectForBounds:(CGRect)bounds
{
return UIEdgeInsetsInsetRect([super textRectForBounds:bounds], _textContainerInset);
}
- (CGRect)editingRectForBounds:(CGRect)bounds
{
return [self textRectForBounds:bounds];
}
#else // [TODO(macOS ISS#2323203)
#pragma mark - NSTextViewDelegate methods
- (void)setScrollEnabled:(BOOL)enabled
{
// Do noting, compatible with multiline textinput
}
- (BOOL)scrollEnabled
{
return NO;
}
- (void)textDidChange:(NSNotification *)notification
{
[super textDidChange:notification];
id<RCTUITextFieldDelegate> delegate = self.delegate;
if ([delegate respondsToSelector:@selector(textFieldDidChange:)]) {
[delegate textFieldDidChange:self];
}
}
- (void)textDidEndEditing:(NSNotification *)notification
{
[super textDidEndEditing:notification];
id<RCTUITextFieldDelegate> delegate = self.delegate;
if ([delegate respondsToSelector:@selector(textFieldEndEditing:)]) {
[delegate textFieldEndEditing:self];
}
}
- (void)textViewDidChangeSelection:(NSNotification *)notification
{
id<RCTUITextFieldDelegate> delegate = self.delegate;
if ([delegate respondsToSelector:@selector(textFieldDidChangeSelection:)]) {
[delegate textFieldDidChangeSelection:self];
}
}
- (BOOL)textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementString:(NSString *)aString
{
id<RCTUITextFieldDelegate> delegate = self.delegate;
if ([delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
return [delegate textField:self shouldChangeCharactersInRange:aRange replacementString:aString];
}
return NO;
}
#endif // ]TODO(macOS ISS#2323203)
#pragma mark - Overrides
#if !TARGET_OS_OSX
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
// Overrides selectedTextRange setter to get notify when selectedTextRange changed.
- (void)setSelectedTextRange:(UITextRange *)selectedTextRange
{
[super setSelectedTextRange:selectedTextRange];
[_textInputDelegateAdapter selectedTextRangeWasSet];
}
#pragma clang diagnostic pop
#endif // !TARGET_OS_OSX
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
- (BOOL)becomeFirstResponder
{
BOOL isFirstResponder = [super becomeFirstResponder];
if (isFirstResponder) {
id<RCTUITextFieldDelegate> delegate = self.delegate;
if ([delegate respondsToSelector:@selector(textFieldBeginEditing:)]) {
// The AppKit -[NSTextField textDidBeginEditing:] notification is only called when the user
// makes the first change to the text in the text field.
// The react-native -[RCTUITextFieldDelegate textFieldBeginEditing:] is intended to be
// called when the text field is focused so call it here in becomeFirstResponder.
[delegate textFieldBeginEditing:self];
}
NSScrollView *scrollView = [self enclosingScrollView];
if (scrollView != nil) {
NSRect visibleRect = [[scrollView documentView] convertRect:self.frame fromView:self];
[[scrollView documentView] scrollRectToVisible:visibleRect];
}
}
return isFirstResponder;
}
#endif // ]TODO(macOS ISS#2323203)
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
- (void)setSelectedTextRange:(UITextRange *)selectedTextRange notifyDelegate:(BOOL)notifyDelegate
{
if (!notifyDelegate) {
// We have to notify an adapter that following selection change was initiated programmatically,
// so the adapter must not generate a notification for it.
[_textInputDelegateAdapter skipNextTextInputDidChangeSelectionEventWithTextRange:selectedTextRange];
}
[super setSelectedTextRange:selectedTextRange];
}
- (void)paste:(id)sender
{
[super paste:sender];
_textWasPasted = YES;
}
#else // [TODO(macOS ISS#2323203)
- (void)setSelectedTextRange:(NSRange)selectedTextRange notifyDelegate:(BOOL)notifyDelegate
{
if (!notifyDelegate) {
// We have to notify an adapter that following selection change was initiated programmatically,
// so the adapter must not generate a notification for it.
[_textInputDelegateAdapter skipNextTextInputDidChangeSelectionEventWithTextRange:selectedTextRange];
}
[[self currentEditor] setSelectedRange:selectedTextRange];
}
- (NSRange)selectedTextRange
{
return [[self currentEditor] selectedRange];
}
#endif // ]TODO(macOS ISS#2323203)
#pragma mark - Layout
- (CGSize)contentSize
{
// Returning size DOES contain `textContainerInset` (aka `padding`).
return self.intrinsicContentSize;
}
- (CGSize)intrinsicContentSize
{
// Note: `placeholder` defines intrinsic size for `<TextInput>`.
NSString *text = self.placeholder ?: @"";
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
CGSize size = [text sizeWithAttributes:[self _placeholderTextAttributes]];
size = CGSizeMake(RCTCeilPixelValue(size.width), RCTCeilPixelValue(size.height));
#else // [TODO(macOS ISS#2323203)
CGSize size = [text sizeWithAttributes:@{NSFontAttributeName: self.font}];
CGFloat scale = self.window.backingScaleFactor;
RCTAssert(scale != 0.0, @"Layout occurs before the view is in a window?");
if (scale == 0) {
scale = [[NSScreen mainScreen] backingScaleFactor];
}
size = CGSizeMake(RCTCeilPixelValue(size.width, scale), RCTCeilPixelValue(size.height, scale));
#endif // ]TODO(macOS ISS#2323203)
size.width += _textContainerInset.left + _textContainerInset.right;
size.height += _textContainerInset.top + _textContainerInset.bottom;
// Returning size DOES contain `textContainerInset` (aka `padding`).
return size;
}
- (CGSize)sizeThatFits:(CGSize)size
{
// All size values here contain `textContainerInset` (aka `padding`).
CGSize intrinsicSize = self.intrinsicContentSize;
return CGSizeMake(MIN(size.width, intrinsicSize.width), MIN(size.height, intrinsicSize.height));
}
#if !TARGET_OS_OSX // [TODO(OSS Candidate ISS#2710739)
- (void)deleteBackward {
id<RCTBackedTextInputDelegate> textInputDelegate = [self textInputDelegate];
if ([textInputDelegate textInputShouldHandleDeleteBackward:self]) {
[super deleteBackward];
}
}
#endif // ]TODO(OSS Candidate ISS#2710739)
@end