Searching for Text Strings

It takes little work to adapt Recipe 6-11 to search for text. To implement search, add a text field to your navigation bar and change the bar button to Find. Use NSString’s rangeOfString:options:range: method to locate the desired string. Be careful: The string you search for must not be nil. After finding the range of your target text (and assuming that the location is not NSNotFound), you can scroll the text view to the right position by calling scrollRangeToVisible:. Pass the range returned by the string method.

Note

NSNotFound is a constant used to indicate that a range was not successfully located. Check the location field after a search to ensure that a valid value was set.

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.