Antecedentes
lo tanto, con un IOS 6 UITextView puede tomar una attributedString, lo que podría ser útil para el resaltado de sintaxis.UITextView attributedText y resaltado de sintaxis
Estoy haciendo algunos patrones de expresiones regulares en -textView:shouldChangeTextInRange:replacementText:
y muchas veces tengo que cambiar el color de una palabra ya escrita. No veo otras opciones aparte de restablecer el texto atribuido, lo que lleva tiempo.
- (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;
}
Esto va en okayish en el simulador, pero en un iPad 3 -setAttributedText
cada toma unos pocos cientos de milisegundos.
He archivado un error en Apple, con la solicitud de poder mutar el texto atribuido. Se marcó como duplicado, por lo que no puedo ver lo que dicen sobre esto.
La pregunta
La pregunta más específica: ¿Cómo se puede cambiar el color de ciertos rangos en un UITextView, con un gran texto multicolor, con un buen rendimiento suficiente para hacerlo en todos los shouldReplaceText...
?
La pregunta más amplia: ¿Cómo se resalta la sintaxis con una UITextView en iOS 6?
También estoy tratando de resaltar el texto que se habla en mi aplicación web de altavoces. Pensé que sería capaz de hacerlo fácilmente con iOS6, pero no pude encontrar la API para cambiar el color de la cadena en cierto rango de forma dinámica. Establecer el texto completo atribuido cada vez lleva demasiado tiempo. –
También archivé un informe de error a Apple. Necesitamos algo como este método. "textView_.attributedText addAttribute: value: font range:" –
Sí, pero apuesto a que nunca veremos una versión mutable del attributeText, ya que textView necesita saber siempre que se cambie la cadena. Esperaría un setter más inteligente, o una versión atribuida de '-replaceRange: withText:' –