CoreText绘图,接收触摸,触摸坐标搞砸了

时间:2010-12-31 21:59:05

标签: iphone cocoa-touch ipad ios core-text

我有一个使用coretext绘制一些文本的视图。一切正常,文字正面朝上,翻转坐标。但是,我还必须回应特定运行的触摸,所以我有以下代码,当点击手势识别器触发时会调用它:

- (void)receivedTap:(UITapGestureRecognizer*)recognizer
{
    CGPoint point = [recognizer locationInView:self];
    NSLog(@"point = %@", NSStringFromCGPoint(point));
    CGContextRef context = UIGraphicsGetCurrentContext();

    CFArrayRef lines = CTFrameGetLines(textFrame);
    CFIndex lineCount = CFArrayGetCount(lines);
    CGPoint origins[lineCount];

    CTFrameGetLineOrigins(textFrame, CFRangeMake(0, 0), origins);
    for(CFIndex idx = 0; idx < lineCount; idx++)
    {
        CTLineRef line = CFArrayGetValueAtIndex(lines, idx);
        CGRect lineBounds = CTLineGetImageBounds(line, context);
        lineBounds.origin.y += origins[idx].y;

        if(CGRectContainsPoint(lineBounds, point))
        {
            CFArrayRef runs = CTLineGetGlyphRuns(line);
            for(CFIndex j = 0; j < CFArrayGetCount(runs); j++)
            {
                CTRunRef run = CFArrayGetValueAtIndex(runs, j);
                NSDictionary* attributes = (NSDictionary*)CTRunGetAttributes(run);
                BOOL result = NO;
                NSURL* url = [attributes objectForKey:kJTextViewDataDetectorLinkKey];
                NSString* phoneNumber = [attributes objectForKey:kJTextViewDataDetectorPhoneNumberKey];
                NSDictionary* addressComponents = [attributes objectForKey:kJTextViewDataDetectorPhoneNumberKey];
                if(url)
                {
                    result = [[UIApplication sharedApplication] openURL:url];
                    return;
                }
                else if(phoneNumber)
                {
                    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", phoneNumber]];
                    result = [[UIApplication sharedApplication] openURL:url];
                    return;
                }
                else if(addressComponents)
                {
                    NSMutableString* address = [NSMutableString string];
                    NSString* temp = nil;
                    if((temp = [addressComponents objectForKey:NSTextCheckingStreetKey]))
                        [address appendString:temp];
                    if((temp = [addressComponents objectForKey:NSTextCheckingCityKey]))
                        [address appendString:[NSString stringWithFormat:@"%@%@", ([address length] > 0) ? @", " : @"", temp]];
                    if((temp = [addressComponents objectForKey:NSTextCheckingStateKey]))
                        [address appendString:[NSString stringWithFormat:@"%@%@", ([address length] > 0) ? @", " : @"", temp]];
                    if((temp = [addressComponents objectForKey:NSTextCheckingZIPKey]))
                        [address appendString:[NSString stringWithFormat:@" %@", temp]];
                    if((temp = [addressComponents objectForKey:NSTextCheckingCountryKey]))
                        [address appendString:[NSString stringWithFormat:@"%@%@", ([address length] > 0) ? @", " : @"", temp]];
                    NSString* urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                    result = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
                    return;
                }
            }
        }
    }
}

哪个适用于第一行,例如,我正在进行一些数据检测的网址。我轻拍它,Mobile Safari就像我期望的那样启动。但是,这仅在项目位于第一行时才有效。我在其他行上看到文字的颜色和下划线(用于地址和电话号码等),我已经验证了相应的数据附加到字符串中的自定义属性,但是当我点击它们时,没有任何事情发生。

我知道我必须以某种方式翻转矩形lineBounds,但我不知道如何去做。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

实际上你需要做的是反过来迭代lines。例如,尝试在此代码中替换:

NSArray* tempLines = (NSArray*)CTFrameGetLines(textFrame);
CFIndex lineCount = [tempLines count];//CFArrayGetCount(lines);
NSMutableArray* lines = [NSMutableArray arrayWithCapacity:lineCount];
for(id elem in [tempLines reverseObjectEnumerator])
    [lines addObject:elem];
CGPoint origins[lineCount];

答案 1 :(得分:0)

最简单的反转方式

        CFArrayRef lines = (CFArrayRef)[[(NSArray *)CTFrameGetLines(frame) reverseObjectEnumerator] allObjects];