배경
따라서 iOS 6에서 UITextView는 구문 강조 표시에 유용 할 수있는 attributedString을 사용할 수 있습니다.UITextView attributedText 및 구문 강조 표시
-textView:shouldChangeTextInRange:replacementText:
에서 일부 정규식 패턴을 사용하고 있으며 이미 입력 한 단어의 색상을 변경해야하는 경우가 있습니다. attributedText를 재설정하는 것 외에는 다른 옵션이 없으므로 시간이 걸립니다.
- (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;
}
이 시뮬레이터에 okayish 실행되지만 아이 패드 3에 각각 -setAttributedText
밀리 초 수백 걸립니다.
나는 attributedText를 변형시킬 수있는 요청으로 Apple에 버그를 제출했습니다. 그것은 중복으로 표시되었으므로, 나는 이것에 대해 그들이 말하는 것을 볼 수 없습니다.
질문
더 구체적인 질문 : 가 어떻게 모든 shouldReplaceText...
에서 그것을 할 충분한 성능, 대형 여러 가지 빛깔 텍스트와 더불어, UITextView에서 특정 범위의 색상을 변경할 수 있습니까?
더 광범위한 질문 : iOS 6에서 UITextView로 구문 강조를 어떻게합니까?
: 여기
은 편리한 카테고리입니다. 나는 iOS6으로 쉽게 할 수있을 것이라고 생각했지만 동적으로 특정 범위의 문자열 색상을 변경하는 API는 찾을 수 없었다. 매번 전체 속성 텍스트를 설정하는 것은 너무 많은 시간이 걸린다. –나는 또한 애플에게 버그 리포트를 제출했다. 우리는이 방법과 같은 것을 필요로합니다. "textView_.attributedText addAttribute : value : font range :" –
예, textView가 문자열이 변경 될 때마다 알 필요가 있기 때문에 변형 된 버전의 attributedText가 표시되지 않을 것입니다. 더 똑똑한 세터 나 UITextInput의 속성 버전 인'-replaceRange : withText :' –