从 UITextView 获取文本的来源 [英] Obtaining origin of Text from UITextView

查看:23
本文介绍了从 UITextView 获取文本的来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-目标:获取 UITextView 中特定文本部分的来源

下面是我正在开发的应用程序屏幕截图的链接.请查看它,因为它更容易解释.图片链接:ScreenShot

Below is a link to a Screenshot of an app I am working on. Please view it as it is easier to explain with it. Image Link: ScreenShot

这是填空风格游戏的开始.我目前正在尝试获取每个下划线的 x,y 坐标.当点击屏幕底部的一个词时,它会移动到下一个可用的下划线空间.

This is a start to a Fill in the Blanks style game. I am currently trying to get the x,y coordinates of each of the underscores. When a word on the bottom of the screen is tapped it will move to the next available underscore space.

目前我已经编写了这段代码来做我需要的事情,但它非常难看,几乎不起作用,而且不是很灵活.见下文:

Currently I have written this code to do what I need, but it is very ugly, barely works AND is not very flexible. See Below:

        // 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){

                     }
         ];

所以我想问的最好的问题是有什么更好的方法来解决这个问题?或者您有什么建议?你会怎么做?

So the the best question for me to ask is What is a better way of going about this? And or do you have any suggestions? How would you go about this?

提前感谢您的时间.

推荐答案

而不是手动搜索每个出现的下划线.使用 NSRegularExpression 这使您的任务变得如此简单和容易.找到匹配的字符串后,使用 NSTextCheckingResult 获取每个匹配字符串的位置.

Rather than manually searching each occurrence of the underscore. make use of NSRegularExpression Which makes your task so simple and easy. After the matched Strings are found then make use of the NSTextCheckingResult to get the location of each matched strings.

更多说明:

您可以使用正则表达式来获取所有出现的下划线.这是使用以下代码获得的.

You can make use of the regular expression inorder get all the occurence of the underscore.This is obtanied using the following code.

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];

获得所有匹配模式后,您可以使用以下代码获取匹配字符串的位置.

Once you get all the Matching Patterns you can get the location of the matched strings using the following code.

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

}
}];

希望这能解决您的所有疑虑!

Hope this answers all your concerns!

这篇关于从 UITextView 获取文本的来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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