-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathTextInsertionUtils.mm
More file actions
39 lines (32 loc) · 1.74 KB
/
Copy pathTextInsertionUtils.mm
File metadata and controls
39 lines (32 loc) · 1.74 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
#import "TextInsertionUtils.h"
#import "UIView+React.h"
#import "EditorManager.h"
#import "ReactNativeRichTextEditorView.h"
@implementation TextInsertionUtils
+ (void)insertText:(NSString*)text inView:(UITextView*)textView at:(NSInteger)index additionalAttributes:(NSDictionary<NSAttributedStringKey, id>*)additionalAttrs {
NSMutableDictionary<NSAttributedStringKey, id> *copiedAttrs = [textView.typingAttributes mutableCopy];
if(additionalAttrs != nullptr) {
[copiedAttrs addEntriesFromDictionary: additionalAttrs];
}
NSAttributedString *newAttrStr = [[NSAttributedString alloc] initWithString:text attributes:copiedAttrs];
[textView.textStorage insertAttributedString:newAttrStr atIndex:index];
[textView reactFocus];
textView.selectedRange = NSMakeRange(index + text.length, 0);
ReactNativeRichTextEditorView *typedEditor = (ReactNativeRichTextEditorView *)[EditorManager sharedManager].currentEditor;
if(typedEditor != nullptr) {
typedEditor->recentlyChangedRange = NSMakeRange(index, text.length);
}
}
+ (void)replaceText:(NSString*)text inView:(UITextView*)textView at:(NSRange)range additionalAttributes:(NSDictionary<NSAttributedStringKey, id>*)additionalAttrs {
[textView.textStorage replaceCharactersInRange:range withString:text];
if(additionalAttrs != nullptr) {
[textView.textStorage addAttributes:additionalAttrs range:NSMakeRange(range.location, [text length])];
}
[textView reactFocus];
textView.selectedRange = NSMakeRange(range.location + text.length, 0);
ReactNativeRichTextEditorView *typedEditor = (ReactNativeRichTextEditorView *)[EditorManager sharedManager].currentEditor;
if(typedEditor != nullptr) {
typedEditor->recentlyChangedRange = NSMakeRange(range.location, text.length);
}
}
@end