-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathLayoutManagerExtension.mm
More file actions
197 lines (162 loc) · 7.6 KB
/
Copy pathLayoutManagerExtension.mm
File metadata and controls
197 lines (162 loc) · 7.6 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#import "LayoutManagerExtension.h"
#import <objc/runtime.h>
#import "EnrichedTextInputView.h"
#import "StyleHeaders.h"
#import "ParagraphsUtils.h"
@implementation NSLayoutManager (LayoutManagerExtension)
static void const *kInputKey = &kInputKey;
- (id)input {
return objc_getAssociatedObject(self, kInputKey);
}
- (void)setInput:(id)value {
objc_setAssociatedObject(
self,
kInputKey,
value,
OBJC_ASSOCIATION_RETAIN_NONATOMIC
);
}
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class myClass = [NSLayoutManager class];
SEL originalSelector = @selector(drawBackgroundForGlyphRange:atPoint:);
SEL swizzledSelector = @selector(my_drawBackgroundForGlyphRange:atPoint:);
Method originalMethod = class_getInstanceMethod(myClass, originalSelector);
Method swizzledMethod = class_getInstanceMethod(myClass, swizzledSelector);
BOOL didAddMethod = class_addMethod(myClass, originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod)
);
if(didAddMethod) {
class_replaceMethod(myClass, swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod)
);
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (void)my_drawBackgroundForGlyphRange:(NSRange)glyphRange atPoint:(CGPoint)origin {
[self my_drawBackgroundForGlyphRange:glyphRange atPoint:origin];
EnrichedTextInputView *typedInput = (EnrichedTextInputView *)self.input;
if(typedInput == nullptr) { return; }
BlockQuoteStyle *bqStyle = typedInput->stylesDict[@([BlockQuoteStyle getStyleType])];
if(bqStyle == nullptr) { return; }
NSRange inputRange = NSMakeRange(0, typedInput->textView.textStorage.length);
// it isn't the most performant but we have to check for all the blockquotes each time and redraw them
NSArray *allBlockquotes = [bqStyle findAllOccurences:inputRange];
for(StylePair *pair in allBlockquotes) {
NSRange paragraphRange = [typedInput->textView.textStorage.string paragraphRangeForRange:[pair.rangeValue rangeValue]];
NSRange paragraphGlyphRange = [self glyphRangeForCharacterRange:paragraphRange actualCharacterRange:nullptr];
[self enumerateLineFragmentsForGlyphRange:paragraphGlyphRange
usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer * _Nonnull textContainer, NSRange glyphRange, BOOL * _Nonnull stop) {
BOOL isRTL = [[RCTI18nUtil sharedInstance] isRTL];
CGFloat width = [typedInput->config blockquoteBorderWidth];
CGFloat height = rect.size.height;
CGFloat x = 0;
if (isRTL) {
x = origin.x + rect.size.width - width;
} else {
x = origin.x;
}
CGFloat y = origin.y + rect.origin.y;
CGRect lineRect = CGRectMake(x, y, width, height);
[[typedInput->config blockquoteBorderColor] setFill];
UIRectFill(lineRect);
}
];
}
UnorderedListStyle *ulStyle = typedInput->stylesDict[@([UnorderedListStyle getStyleType])];
OrderedListStyle *olStyle = typedInput->stylesDict[@([OrderedListStyle getStyleType])];
if(ulStyle == nullptr || olStyle == nullptr) { return; }
// also not the most performant but we redraw all the lists
NSMutableArray *allLists = [[NSMutableArray alloc] init];
[allLists addObjectsFromArray:[ulStyle findAllOccurences:inputRange]];
[allLists addObjectsFromArray:[olStyle findAllOccurences:inputRange]];
for(StylePair *pair in allLists) {
NSParagraphStyle *pStyle = (NSParagraphStyle *)pair.styleValue;
NSDictionary *markerAttributes = @{
NSFontAttributeName: [typedInput->config orderedListMarkerFont],
NSForegroundColorAttributeName: [typedInput->config orderedListMarkerColor]
};
NSArray *paragraphs = [ParagraphsUtils getSeparateParagraphsRangesIn:typedInput->textView range:[pair.rangeValue rangeValue]];
for(NSValue *paragraph in paragraphs) {
NSRange paragraphGlyphRange = [self glyphRangeForCharacterRange:[paragraph rangeValue] actualCharacterRange:nullptr];
[self enumerateLineFragmentsForGlyphRange:paragraphGlyphRange
usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer *container, NSRange lineGlyphRange, BOOL *stop) {
NSString *marker = [self markerForList:pStyle.textLists.firstObject charIndex:[self characterIndexForGlyphAtIndex:lineGlyphRange.location] input:typedInput];
BOOL isRTL = [[RCTI18nUtil sharedInstance] isRTL];
CGFloat textStartLeft = usedRect.origin.x;
CGFloat textEndRight = usedRect.origin.x + usedRect.size.width;
if(pStyle.textLists.firstObject.markerFormat == NSTextListMarkerDecimal) {
CGFloat gapWidth = [typedInput->config orderedListGapWidth];
CGFloat markerWidth = [marker sizeWithAttributes:markerAttributes].width;
CGFloat markerX = 0;
if (isRTL) {
markerX = textEndRight + gapWidth;
} else {
markerX = textStartLeft - gapWidth - markerWidth / 2;
}
[marker drawAtPoint:CGPointMake(markerX, usedRect.origin.y + origin.y) withAttributes:markerAttributes];
} else {
CGFloat gapWidth = [typedInput->config unorderedListGapWidth];
CGFloat bulletSize = [typedInput->config unorderedListBulletSize];
CGFloat centerY = CGRectGetMidY(usedRect);
CGFloat bulletX = 0;
if (isRTL) {
bulletX = textEndRight + gapWidth + (bulletSize / 2.0);
} else {
bulletX = textStartLeft - gapWidth - (bulletSize / 2.0);
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context); {
[[typedInput->config unorderedListBulletColor] setFill];
CGContextAddArc(context, bulletX, centerY, bulletSize/2, 0, 2 * M_PI, YES);
CGContextFillPath(context);
}
CGContextRestoreGState(context);
}
// only first line of a list gets its marker drawn
*stop = YES;
}
];
}
}
}
- (NSString *)markerForList:(NSTextList *)list charIndex:(NSUInteger)index input:(EnrichedTextInputView *)input {
if(list.markerFormat == NSTextListMarkerDecimal) {
NSString *fullText = input->textView.textStorage.string;
NSInteger itemNumber = 1;
NSRange currentParagraph = [fullText paragraphRangeForRange:NSMakeRange(index, 0)];
if(currentParagraph.location > 0) {
OrderedListStyle *olStyle = input->stylesDict[@([OrderedListStyle getStyleType])];
NSInteger prevParagraphsCount = 0;
NSInteger recentParagraphLocation = [fullText paragraphRangeForRange:NSMakeRange(currentParagraph.location - 1, 0)].location;
// seek for previous lists
while(true) {
if([olStyle detectStyle:NSMakeRange(recentParagraphLocation, 0)]) {
prevParagraphsCount += 1;
if(recentParagraphLocation > 0) {
recentParagraphLocation = [fullText paragraphRangeForRange:NSMakeRange(recentParagraphLocation - 1, 0)].location;
} else {
break;
}
} else {
break;
}
}
itemNumber = prevParagraphsCount + 1;
}
BOOL isRTL = [[RCTI18nUtil sharedInstance] isRTL];
if (isRTL) {
return [NSString stringWithFormat:@".%ld", (long)(itemNumber)];
} else {
return [NSString stringWithFormat:@"%ld.", (long)(itemNumber)];
}
} else {
return @"•";
}
}
@end