UITextView attributeText and syntax highlighting

Background

So, with iOS 6, UITextView can take the String attribute, which can be useful for syntax highlighting.

I make several regex patterns in -textView:shouldChangeTextInRange:replacementText: and often I need to change the color of an already entered word. I see no other options than resetting the Text attribute, which takes time.

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { //A context will allow us to not call -attributedText on the textView, which is slow. //Keep context up to date [self.context replaceCharactersInRange:range withAttributedString:[[NSAttributedString alloc] initWithString:text attributes:self.textView.typingAttributes]]; // […] self.textView.scrollEnabled = FALSE; [self.context setAttributes:self.defaultStyle range:NSMakeRange(0, self.context.length)]; [self refresh]; //Runs regex-patterns in the context textView.attributedText = self.context; self.textView.selectedRange = NSMakeRange(range.location + text.length, 0); self.textView.scrollEnabled = TRUE; return FALSE; } 

This works fine on the simulator, but on iPad 3, each -setAttributedText takes several hundred milliseconds.

I filed an Apple error requesting to mutate the Text attribute. It has been marked as a duplicate, so I cannot understand what they are saying about it.

Question

A more specific question: How to change the color of certain ranges in a UITextView with large multi-colored text with enough performance to do this in each shouldReplaceText... ?

The broader question is: How do I make syntax highlighting using UITextView in iOS 6?

+6
source share
2 answers

Attributes that are assigned textText attributes should be rounded to / from HTML, so this is really not optimal for implementing syntax with a selected text representation. On iOS 6, you probably want to use CoreText directly.

0
source

I ran into the same problem for my Zap-Guitar (No-Strings-Attached) application, where I allow users to enter / insert / edit their own songs, and the application highlights recognized chords.

Yes, a true apple uses html-writer and parser to display a text attribute. A wonderful explanation behind the scenes can be found here: http://www.cocoanetics.com/2012/12/uitextview-caught-with-trousers-down/

The only solution I found for this problem is to not use attribute text, which is redundant for syntax highlighting.

Instead, I went back to the good old plaintext UITextView and added buttons to the text view where it was highlighted. To calculate the frames of the buttons, I used this answer: How to find a position or access any word in text form and put buttons on it?

This reduced CPU usage by 30% (give or take).

Here is a convenient category:

 @implementation UITextView (WithButtons) - (CGRect)frameForTextRange:(NSRange)range { UITextPosition *beginning = self.beginningOfDocument; UITextPosition *start = [self positionFromPosition:beginning offset:range.location]; UITextPosition *end = [self positionFromPosition:start offset:range.length]; UITextRange *textRange = [self textRangeFromPosition:start toPosition:end]; CGRect rect = [self firstRectForRange:textRange]; return [self convertRect:rect fromView:self.textInputView]; } @end 
+1
source

Source: https://habr.com/ru/post/925995/


All Articles