-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathRCTUITextView+Markdown.mm
42 lines (35 loc) · 1.48 KB
/
RCTUITextView+Markdown.mm
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
#import <RNLiveMarkdown/RCTUITextView+Markdown.h>
#import <RNLiveMarkdown/RCTMarkdownUtils.h>
#import <objc/message.h>
@implementation RCTUITextView (Markdown)
- (void)setMarkdownUtils:(RCTMarkdownUtils *)markdownUtils {
objc_setAssociatedObject(self, @selector(getMarkdownUtils), markdownUtils, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (RCTMarkdownUtils *)getMarkdownUtils {
return objc_getAssociatedObject(self, @selector(getMarkdownUtils));
}
- (void)markdown_textDidChange
{
RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils];
if (markdownUtils != nil) {
UITextRange *range = self.selectedTextRange;
super.attributedText = [markdownUtils parseMarkdown:self.attributedText withAttributes:self.defaultTextAttributes];
[super setSelectedTextRange:range]; // prevents cursor from jumping at the end when typing in the middle of the text
self.typingAttributes = self.defaultTextAttributes; // removes indent in new line when typing after blockquote
}
// Call the original method
[self markdown_textDidChange];
}
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class cls = [self class];
SEL originalSelector = @selector(textDidChange);
SEL swizzledSelector = @selector(markdown_textDidChange);
Method originalMethod = class_getInstanceMethod(cls, originalSelector);
Method swizzledMethod = class_getInstanceMethod(cls, swizzledSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);
});
}
@end