如何实现对UITextView的搜索功能? [英] How to implement search function to UITextView?

查看:201
本文介绍了如何实现对UITextView的搜索功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有40多个视图,分别具有各自的UITexView.我想实现一个搜索功能,该功能允许用户在UITextViews中进行搜索.实际上,我什至不知道如何实现1 UITextView的搜索功能.因此,我完全不知道是否可能.

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.

感谢您的建议.

推荐答案

这是一个非常简单的任务(我可以说明显吗?).只是想一想.在文本视图中搜索是什么意思?好吧,这意味着获取其文本,并查看特定的字符串(要搜索的字符串)是否为文本的子字符串,如果是,则位于其中.假设您使用UISearchBar来保存要搜索的文本:

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.
}

这篇关于如何实现对UITextView的搜索功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆