@@ -49,23 +49,6 @@ - (void)addAttributes:(NSRange)range {
4949 offset += 1 ;
5050 }
5151
52- [_input->textView.textStorage enumerateAttribute: NSFontAttributeName
53- inRange: pRange
54- options: 0
55- usingBlock: ^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
56- UIFont *font = (UIFont *)value;
57- if (font != nullptr ) {
58- UIFont *newFont = [[[_input->config monospacedFont ] withFontTraits: font] setSize: font.pointSize];
59- [_input->textView.textStorage addAttribute: NSFontAttributeName value: newFont range: range];
60- }
61- }];
62-
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-
6952 [_input->textView.textStorage enumerateAttribute: NSParagraphStyleAttributeName inRange: pRange options: 0
7053 usingBlock: ^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
7154 NSMutableParagraphStyle *pStyle = [(NSParagraphStyle *)value mutableCopy ];
@@ -91,11 +74,6 @@ - (void)addAttributes:(NSRange)range {
9174 NSMutableParagraphStyle *pStyle = [typingAttrs[NSParagraphStyleAttributeName ] mutableCopy ];
9275 pStyle.textLists = @[codeBlockList];
9376 typingAttrs[NSParagraphStyleAttributeName ] = pStyle;
94- typingAttrs[NSForegroundColorAttributeName ] = [_input->config codeBlockFgColor ];
95- UIFont *currentFont = typingAttrs[NSFontAttributeName ];
96- if (currentFont != nullptr ) {
97- typingAttrs[NSFontAttributeName ] = [[[_input->config monospacedFont ] withFontTraits: currentFont] setSize: currentFont.pointSize];
98- }
9977
10078 _input->textView .typingAttributes = typingAttrs;
10179}
@@ -111,23 +89,8 @@ - (void)removeAttributes:(NSRange)range {
11189
11290 for (NSValue *value in paragraphs) {
11391 NSRange pRange = [value rangeValue ];
114- [_input->textView.textStorage enumerateAttribute: NSFontAttributeName inRange: pRange options: 0
115- usingBlock: ^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
116- UIFont *font = (UIFont *)value;
117- if (font != nullptr ) {
118- UIFont *newFont = [[[_input->config primaryFont ] withFontTraits: font] setSize: font.pointSize];
119- [_input->textView.textStorage addAttribute: NSFontAttributeName value: newFont range: range];
120- }
121- }
122- ];
12392
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-
130- [_input->textView.textStorage enumerateAttribute: NSParagraphStyleAttributeName inRange: range options: 0
93+ [_input->textView.textStorage enumerateAttribute: NSParagraphStyleAttributeName inRange: pRange options: 0
13194 usingBlock: ^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
13295 NSMutableParagraphStyle *pStyle = [(NSParagraphStyle *)value mutableCopy ];
13396 pStyle.textLists = @[];
@@ -144,11 +107,6 @@ - (void)removeAttributes:(NSRange)range {
144107 pStyle.textLists = @[];
145108
146109 typingAttrs[NSParagraphStyleAttributeName ] = pStyle;
147- typingAttrs[NSForegroundColorAttributeName ] = [_input->config primaryColor ];
148- UIFont *currentFont = typingAttrs[NSFontAttributeName ];
149- if (currentFont != nullptr ) {
150- typingAttrs[NSFontAttributeName ] = [[[_input->config primaryFont ] withFontTraits: currentFont] setSize: currentFont.pointSize];
151- }
152110
153111 _input->textView .typingAttributes = typingAttrs;
154112}
@@ -175,7 +133,7 @@ - (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text {
175133
176134- (BOOL )styleCondition : (id _Nullable)value : (NSRange )range {
177135 NSParagraphStyle *paragraph = (NSParagraphStyle *)value;
178- return paragraph != nullptr && paragraph.textLists .count == 1 && [paragraph.textLists.firstObject.markerFormat isEqual: @" codeblock" ];
136+ return paragraph != nullptr && paragraph.textLists .count == 1 && [paragraph.textLists.firstObject.markerFormat isEqual: @" codeblock" ];
179137}
180138
181139- (BOOL )detectStyle : (NSRange )range {
@@ -210,4 +168,53 @@ - (BOOL)anyOccurence:(NSRange)range {
210168 ];
211169}
212170
171+ - (void )manageCodeBlockFontAndColor {
172+ if ([[_input->config codeBlockFgColor ] isEqualToColor: [_input->config primaryColor ]]) {
173+ return ;
174+ }
175+
176+ NSRange wholeRange = NSMakeRange (0 , _input->textView .textStorage .string .length );
177+ NSArray *paragraphs = [ParagraphsUtils getSeparateParagraphsRangesIn: _input->textView range: wholeRange];
178+
179+ for (NSValue *pValue in paragraphs) {
180+ NSRange paragraphRange = [pValue rangeValue ];
181+ BOOL selfDetected = [self detectStyle: paragraphRange];
182+
183+ [_input->textView.textStorage enumerateAttribute: NSFontAttributeName inRange: paragraphRange options: 0
184+ usingBlock: ^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
185+ UIFont *currentFont = (UIFont *)value;
186+ UIFont *newFont = nullptr ;
187+
188+ BOOL isCodeFont = [[currentFont familyName ] isEqualToString: [[_input->config monospacedFont ] familyName ]];
189+
190+ if (isCodeFont && !selfDetected) {
191+ newFont = [[[_input->config primaryFont ] withFontTraits: currentFont] setSize: currentFont.pointSize];
192+ } else if (!isCodeFont && selfDetected) {
193+ newFont = [[[_input->config monospacedFont ] withFontTraits: currentFont] setSize: currentFont.pointSize];
194+ }
195+
196+ if (newFont != nullptr ) {
197+ [_input->textView.textStorage addAttribute: NSFontAttributeName value: newFont range: range];
198+ }
199+ }];
200+
201+ [_input->textView.textStorage enumerateAttribute: NSForegroundColorAttributeName inRange: paragraphRange options: 0
202+ usingBlock: ^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
203+ UIColor *newColor = nullptr ;
204+ BOOL colorApplied = [(UIColor *)value isEqualToColor: [_input->config codeBlockFgColor ]];
205+
206+ if (colorApplied && !selfDetected) {
207+ newColor = [_input->config primaryColor ];
208+ } else if (!colorApplied && selfDetected) {
209+ newColor = [_input->config codeBlockFgColor ];
210+ }
211+
212+ if (newColor != nullptr ) {
213+ [_input->textView.textStorage addAttribute: NSForegroundColorAttributeName value: newColor range: range];
214+ }
215+ }
216+ ];
217+ }
218+ }
219+
213220@end
0 commit comments