3
大きなテキストのテキストビューがあり、その中に文字列(検索)がすべて含まれていることを確認し、検索を押すたびにtextViewを範囲にスクロールする現在のオカレンスTextViewで文字列を検索する
テキストビューで前方検索を行うには感謝
大きなテキストのテキストビューがあり、その中に文字列(検索)がすべて含まれていることを確認し、検索を押すたびにtextViewを範囲にスクロールする現在のオカレンスTextViewで文字列を検索する
テキストビューで前方検索を行うには感謝
は、次のスニペットを使用する -
NSRange textRange;
NSRange searchRange = NSMakeRange(0, [textView.text length]);
textRange = [textView.text rangeOfString:searchString
options:NSCaseInsensitiveSearch
range:searchRange];
if (textRange.location == NSNotFound) {
// Not there
} else {
textView.selectedRange = textRange;
[textView scrollRangeToVisible:textRange];
}
基本的に、我々はテキストを検索してからselectedRange
を使用してテキストを強調するためにNSString
のrangeOfString:options:range:
メソッドを使用し、 scrollRangeToVisible:
を使用して見えるようにします。
今すぐ見つかったら、検索範囲を変更して次のオカレンスを見つけることができます。
if (textRange.location + textRange.length <= [textView.text length]) {
searchRange.location = textRange.location + textRange.length;
searchRange.length = [textView.text length] - searchRange.location;
textRange = [textView.text rangeOfString:searchString
options:NSCaseInsensitiveSearch
range:searchRange];
/* Validate search result & highlight the text */
} else {
// No more text to search.
}
また
searchRange = NSMakeRange(0, textRange.location);
を宣言してからoptions
で(NSCaseInsensitiveSearch|NSBackwardsSearch)
を渡すことで後方検索することができます。
ありがとう、私は家に帰るときに試してみる – BRIT