Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在UITextView中获取相同文本(单词)的位置_Iphone_Ios_Uitextview - Fatal编程技术网

Iphone 如何在UITextView中获取相同文本(单词)的位置

Iphone 如何在UITextView中获取相同文本(单词)的位置,iphone,ios,uitextview,Iphone,Ios,Uitextview,我在文本视图中工作。在其中,我必须突出显示dat特定文本视图中的相同文本。例如,在文本视图的以下文本中,我必须突出显示所有“Apple”单词 iPhone的开发始于2004年,当时苹果公司开始召集1000名员工参与高度机密的“紫色计划”[27],其中包括iPhone的设计师乔纳森·艾夫爵士。[28]苹果公司首席执行官史蒂夫·乔布斯将最初的重点从平板电脑(如iPad)转向了手机。[29]苹果公司在当时与AT&T Mobility Cingular Wireless秘密合作的过程中开发了该设备,预计

我在文本视图中工作。在其中,我必须突出显示dat特定文本视图中的相同文本。例如,在文本视图的以下文本中,我必须突出显示所有“Apple”单词

iPhone的开发始于2004年,当时苹果公司开始召集1000名员工参与高度机密的“紫色计划”[27],其中包括iPhone的设计师乔纳森·艾夫爵士。[28]苹果公司首席执行官史蒂夫·乔布斯将最初的重点从平板电脑(如iPad)转向了手机。[29]苹果公司在当时与AT&T Mobility Cingular Wireless秘密合作的过程中开发了该设备,预计30个月内开发成本为1.5亿美元。[30]

我使用这段代码在dat文本视图中查找特定“Apple”字符串的位置

NSString *sel_text = @"Apple";
 NSRange range = [subTextView.text rangeOfString:sel_text];

subTextView.selectedRange =range;

    UITextRange *textRange = [subTextView selectedTextRange];
 CGRect rect = [subTextView firstRectForRange:textRange];
这只帮助我找到“苹果”的第一个位置,我无法找到该字符串的下一个位置

所以请帮我找到所有的位置


谢谢

您可以使用
rangeOfString:options:range:
selector在一个简单的循环中遍历整个字符串。比如说,

NSString *sel_text = @"Apple";
NSRange range = [data rangeOfString:sel_text];
while(range.location != NSNotFound) {

    subTextView.selectedRange =range;
    UITextRange *textRange = [subTextView selectedTextRange];
    CGRect rect = [subTextView firstRectForRange:textRange];

    range = [data rangeOfString:sel_text options:0 range:NSMakeRange(range.location + sel_text.length, data.length - range.location - sel_text.length)];
}

请看一下代码中的“NSRegularExpression”.bro,这一行。。。。range=[数据范围字符串:选择文本选项:0范围:NSMakeRange(range.location+sel_text.length,data.length-range.location-sel_text.length)];我得到range.location=a垃圾值&range.length=0,所以它不起作用,兄弟