问题
I have more than 40 views with their own respective UITexView
. I want to implement a search function that allows the user to search across the UITextViews
. Actually, I do not even know how to implement search function for 1 UITextView
. Therefore I do not know if it is possible at all.
I have searched the web and looked for it here but did not find what I looked for.
I appreciate your advice.
回答1:
This is a pretty easy (may I say obvious?) task. Just think a bit about it. What does searching in a text view mean? Well, it means getting its text and see if a particular string (the one which is searched for) is the substring of the text, and if so, where it is inside. Assuming you use a UISearchBar to hold the text to be searched for:
NSString *subs = searchBar.text;
NSString *wholeText = textView.text;
NSRange r = [wholeText rangeOfString:subs];
if (r.location == NSNotFound)
{
// The term to be searched couldn't be found...
}
else
{
// The string to be searched for is in the text view, r.location contains where exactly it is.
}
回答2:
You can search for text in TextView like this:
[TextView.text rangeOfString:]
There's no built in controller for searching UITextView, you have to implement this functionality by yourself with above function.
来源:https://stackoverflow.com/questions/11914282/how-to-implement-search-function-to-uitextview