Iphone 从UITextView获取文本的来源
-目标:在UITextView中获取特定文本部分的来源 下面是我正在开发的一个应用程序的屏幕截图链接。请查看它,因为它更容易解释。 图像链接: 这是一个填空风格游戏的开始。我目前正在尝试获取每个下划线的x,y坐标。当点击屏幕底部的单词时,它将移动到下一个可用的下划线空间 目前我已经写了这段代码来做我需要的事情,但它非常难看,几乎不能工作,也不是很灵活。见下文:Iphone 从UITextView获取文本的来源,iphone,ios,objective-c,uitextview,xcode4.5,Iphone,Ios,Objective C,Uitextview,Xcode4.5,-目标:在UITextView中获取特定文本部分的来源 下面是我正在开发的一个应用程序的屏幕截图链接。请查看它,因为它更容易解释。 图像链接: 这是一个填空风格游戏的开始。我目前正在尝试获取每个下划线的x,y坐标。当点击屏幕底部的单词时,它将移动到下一个可用的下划线空间 目前我已经写了这段代码来做我需要的事情,但它非常难看,几乎不能工作,也不是很灵活。见下文: // self.mainTextView is where the text with the underscores
// self.mainTextView is where the text with the underscores is coming from
NSString *text = self.mainTextView.text;
NSString *substring = [text substringToIndex:[text rangeOfString:@"__________"].location];
CGSize size = [substring sizeWithFont:self.mainTextView.font];
CGPoint p = CGPointMake((int)size.width % (int)self.mainTextView.frame.size.width, ((int)size.width / (int)self.mainTextView.frame.size.width) * size.height);
// What is going on here is for some reason everytime there is a new
// line my x coordinate is offset by what seems to be 10 pixels...
// So was my ugly fix for it..
// The UITextView width is 280
CGRect mainTextFrame = [self.mainTextView frame];
p.x = p.x + mainTextFrame.origin.x + 9;
if ((int)size.width > 280) {
NSLog(@"width: 280");
p.x = p.x + mainTextFrame.origin.x + 10;
}
if ((int)size.width > 560) {
NSLog(@"width: 560");
p.x = p.x + mainTextFrame.origin.x + 12;
}
if ((int)size.width > 840) {
p.x = p.x + mainTextFrame.origin.x + 14;
}
p.y = p.y + mainTextFrame.origin.y + 5;
// Sender is the button that was pressed
newFrame = [sender frame];
newFrame.origin = p;
[UIView animateWithDuration:0.2
delay:0
options:UIViewAnimationOptionAllowAnimatedContent|UIViewAnimationCurveEaseInOut
animations:^{
[sender setFrame:newFrame];
}
completion:^(BOOL finished){
}
];
所以对我来说最好的问题是
有什么更好的方法吗?或者你有什么建议吗?你会怎么做
提前感谢您的时间。而不是手动搜索每次出现的下划线。使用NSRegularExpression,它使您的任务变得简单易行。找到匹配的字符串后,使用
NSTextCheckingResult
获取每个匹配字符串的位置
更多解释:
您可以使用正则表达式来获取下划线的所有出现
NSError *error = NULL;
NSString *pattern = @"_*_"; // pattern to search underscore.
NSString *string = self.textView.text;
NSRange range = NSMakeRange(0, string.length);
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches = [regex matchesInString:string options:NSMatchingProgress range:range];
[matches enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[NSTextCheckingResult class]])
{
NSTextCheckingResult *match = (NSTextCheckingResult *)obj;
CGRect rect = [self frameOfTextRange:match.range inTextView:self.textView];
//get location of all the matched strings.
}
}];
获得所有匹配模式后,可以使用以下代码获得匹配字符串的位置
NSError *error = NULL;
NSString *pattern = @"_*_"; // pattern to search underscore.
NSString *string = self.textView.text;
NSRange range = NSMakeRange(0, string.length);
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches = [regex matchesInString:string options:NSMatchingProgress range:range];
[matches enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[NSTextCheckingResult class]])
{
NSTextCheckingResult *match = (NSTextCheckingResult *)obj;
CGRect rect = [self frameOfTextRange:match.range inTextView:self.textView];
//get location of all the matched strings.
}
}];
希望这能回答你所有的担忧 你想知道uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu!我不熟悉日常用语。在
NSArray*matches
上,我收到一个警告,如下所示:xcode从枚举类型'enum NSMatchingFlags'隐式转换为不同类型的NSMatchingOptions
只需使用NSMatchingReportCompletion
更改选项的值,另一个问题是行CGRect rect=[self-frameoftextange:match.ra…..
self应该是什么?文本视图?如果这是一个愚蠢的问题,很抱歉,只需要确定一下嘿Vinayak Kini,别担心,我没有忘记你,只是在用我的代码测试你的代码。它很有效,非常感谢你花时间:-)如果这让你有点沮丧,我很抱歉,但是我得从某个地方开始了。再次感谢老兄!我现在没有任何其他问题。我更喜欢尝试自己解决问题,然后如果我到了一个无法通过的地方,我会问问题。但也许我们会再次相遇。再次感谢老兄!:D