How to implement search function to UITextView?

拥有回忆 提交于 2019-12-19 04:45:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!