私はUITextView
を持っています。ユーザーがテキストを入力しているときに、その場でテキストの書式を設定します。私はテキストビューからテキストを取ると、それのからNSAttributedString
を作る:構文の強調表示のような何か...私はUITextView
を使用したいそのためにUITextViewsのテキストを属性付きの文字列に置き換えます。
...
すべてが一つの問題を期待して正常に動作します。この属性付き文字列を編集してtextView.attributedText
として返します。
これは、ユーザーが入力するたびに発生します。だから私はattributedText
への編集の前にselectedTextRange
を覚えておき、後でそれを元に戻して、ユーザが前に入力したところで入力を続けることができるようにしなければならない。唯一の問題は、テキストがスクロールするのに十分な長さになると、ゆっくりと入力するとUITextView
が上部にスクロールし始めることです。ここで
は、いくつかのサンプルコードです:
- (void)formatTextInTextView:(UITextView *)textView
{
NSRange selectedRange = textView.selectedRange;
NSString *text = textView.text;
// This will give me an attributedString with the base text-style
NSMutableAttributedString *attributedString = [self attributedStringFromString:text];
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error];
NSArray *matches = [regex matchesInString:text
options:0
range:NSMakeRange(0, text.length)];
for (NSTextCheckingResult *match in matches)
{
NSRange matchRange = [match rangeAtIndex:0];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:matchRange];
}
textView.attributedText = attributedString;
textView.selectedRange = selectedRange;
}
は直接CoreTextを使用せずに任意の解決策はありますか?私はUITextView
のテキストの選択などが好きです....
@ナナフ投稿を編集してSOを改善しようとしてくれてありがとう。しかし、[本当に理由はありません](http://meta.stackexchange.com/q/158564/169503)あなたが思っている任意の大胆な強調のために[http://stackoverflow.com/review/suggested-edits/] 2242818)[紹介](http://stackoverflow.com/review/suggested-edits/2247096)[様々な](http:// http://stackoverflow.com/page/subgested-edits/2247096) [投稿](http://stackoverflow.com/review/suggested-edits/2247183)。それは騒音を導入するだけであり、実際には積極的に有害であるとみなすことができる。 –