@@ -60,6 +60,12 @@ - (void)addAttributes:(NSRange)range {
6060 }
6161 }];
6262
63+ [_input->textView.textStorage enumerateAttribute: NSForegroundColorAttributeName inRange: pRange options: 0
64+ usingBlock: ^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
65+ [_input->textView.textStorage addAttribute: NSForegroundColorAttributeName value: [_input->config codeBlockFgColor ] range: range];
66+ }
67+ ];
68+
6369 [_input->textView.textStorage enumerateAttribute: NSParagraphStyleAttributeName inRange: pRange options: 0
6470 usingBlock: ^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
6571 NSMutableParagraphStyle *pStyle = [(NSParagraphStyle *)value mutableCopy ];
@@ -85,6 +91,7 @@ - (void)addAttributes:(NSRange)range {
8591 NSMutableParagraphStyle *pStyle = [typingAttrs[NSParagraphStyleAttributeName ] mutableCopy ];
8692 pStyle.textLists = @[codeBlockList];
8793 typingAttrs[NSParagraphStyleAttributeName ] = pStyle;
94+ typingAttrs[NSForegroundColorAttributeName ] = [_input->config codeBlockFgColor ];
8895 UIFont *currentFont = typingAttrs[NSFontAttributeName ];
8996 if (currentFont != nullptr ) {
9097 typingAttrs[NSFontAttributeName ] = [[[_input->config monospacedFont ] withFontTraits: currentFont] setSize: currentFont.pointSize];
@@ -114,6 +121,12 @@ - (void)removeAttributes:(NSRange)range {
114121 }
115122 ];
116123
124+ [_input->textView.textStorage enumerateAttribute: NSForegroundColorAttributeName inRange: pRange options: 0
125+ usingBlock: ^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
126+ [_input->textView.textStorage addAttribute: NSForegroundColorAttributeName value: [_input->config primaryColor ] range: range];
127+ }
128+ ];
129+
117130 [_input->textView.textStorage enumerateAttribute: NSParagraphStyleAttributeName inRange: range options: 0
118131 usingBlock: ^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
119132 NSMutableParagraphStyle *pStyle = [(NSParagraphStyle *)value mutableCopy ];
@@ -131,6 +144,7 @@ - (void)removeAttributes:(NSRange)range {
131144 pStyle.textLists = @[];
132145
133146 typingAttrs[NSParagraphStyleAttributeName ] = pStyle;
147+ typingAttrs[NSForegroundColorAttributeName ] = [_input->config primaryColor ];
134148 UIFont *currentFont = typingAttrs[NSFontAttributeName ];
135149 if (currentFont != nullptr ) {
136150 typingAttrs[NSFontAttributeName ] = [[[_input->config primaryFont ] withFontTraits: currentFont] setSize: currentFont.pointSize];
@@ -143,6 +157,22 @@ - (void)removeTypingAttributes {
143157 [self removeAttributes: _input->textView.selectedRange];
144158}
145159
160+ - (BOOL )handleBackspaceInRange : (NSRange )range replacementText : (NSString *)text {
161+ if ([self detectStyle: _input->textView.selectedRange] && text.length == 0 ) {
162+ // backspace while the style is active
163+
164+ NSRange paragraphRange = [_input->textView.textStorage.string paragraphRangeForRange: _input->textView.selectedRange];
165+
166+ if (NSEqualRanges (_input->textView .selectedRange , NSMakeRange (0 , 0 ))) {
167+ // a backspace on the very first input's line quote
168+ // it doesn't run textVieDidChange so we need to manually remove attributes
169+ [self removeAttributes: paragraphRange];
170+ return YES ;
171+ }
172+ }
173+ return NO ;
174+ }
175+
146176- (BOOL )styleCondition : (id _Nullable)value : (NSRange )range {
147177 NSParagraphStyle *paragraph = (NSParagraphStyle *)value;
148178 return paragraph != nullptr && paragraph.textLists .count == 1 && [paragraph.textLists.firstObject.markerFormat isEqual: @" codeblock" ];
@@ -180,37 +210,4 @@ - (BOOL)anyOccurence:(NSRange)range {
180210 ];
181211}
182212
183- - (void )manageCodeBlockColor {
184- if ([[_input->config codeBlockFgColor ] isEqualToColor: [_input->config primaryColor ]]) {
185- return ;
186- }
187-
188- NSRange wholeRange = NSMakeRange (0 , _input->textView .textStorage .string .length );
189-
190- NSArray *paragraphs = [ParagraphsUtils getSeparateParagraphsRangesIn: _input->textView range: wholeRange];
191- for (NSValue *pValue in paragraphs) {
192- NSRange paragraphRange = [pValue rangeValue ];
193- BOOL selfDetected = [self detectStyle: paragraphRange];
194-
195- [_input->textView.textStorage enumerateAttribute: NSForegroundColorAttributeName inRange: paragraphRange options: 0
196- usingBlock: ^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
197- UIColor *newColor = nullptr ;
198- BOOL colorApplied = [(UIColor *)value isEqualToColor: [_input->config codeBlockFgColor ]];
199-
200- if (colorApplied && !selfDetected) {
201- newColor = [_input->config primaryColor ];
202- } else if (!colorApplied && selfDetected) {
203- newColor = [_input->config codeBlockFgColor ];
204- }
205-
206- if (newColor != nullptr ) {
207- [_input->textView.textStorage addAttribute: NSForegroundColorAttributeName value: newColor range: range];
208- [_input->textView.textStorage addAttribute: NSUnderlineColorAttributeName value: newColor range: range];
209- [_input->textView.textStorage addAttribute: NSStrikethroughColorAttributeName value: newColor range: range];
210- }
211- }
212- ];
213- }
214- }
215-
216213@end
0 commit comments