iphone - 使用 CoreText 和 touches 创建可点击的 Action

标签 iphone objective-c ipad core-text

我在 View 中有一些代码使用 CoreText 绘制一些属性文本。在这里,我正在搜索 url 并将它们设为蓝色。这个想法是不要为了获得可点击的链接而引入 UIWebView 的所有开销。一旦用户点击该链接(而不是整个表格 View 单元格),我想触发一个委托(delegate)方法,该方法将用于呈现一个模态视图,其中包含一个指向该 url 的 WebView 。

我将路径和字符串本身保存为 View 的实例变量,绘图代码发生在 -drawRect: 中(为简洁起见,我将其省略)。

然而,我的触摸处理程序虽然不完整,但并未打印出我期望的结果。如下:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    CGContextRef context = UIGraphicsGetCurrentContext();

    NSLog(@"attribString = %@", self.attribString);
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.attribString);
    CTFrameRef ctframe = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, self.attribString.length), attribPath, NULL);

    CFArrayRef lines = CTFrameGetLines(ctframe);
    for(CFIndex i = 0; i < CFArrayGetCount(lines); i++)
    {
        CTLineRef line = CFArrayGetValueAtIndex(lines, i);
        CGRect lineBounds = CTLineGetImageBounds(line, context);

        // Check if tap was on our line
        if(CGRectContainsPoint(lineBounds, point))
        {
            NSLog(@"Tapped line");
            CFArrayRef runs = CTLineGetGlyphRuns(line);
            for(CFIndex j = 0; j < CFArrayGetCount(runs); j++)
            {
                CTRunRef run = CFArrayGetValueAtIndex(runs, j);
                CFRange urlStringRange = CTRunGetStringRange(run); 
                CGRect runBounds = CTRunGetImageBounds(run, context, urlStringRange);

                if(CGRectContainsPoint(runBounds, point))
                {
                    NSLog(@"Tapped run");
                    CFIndex* buffer = malloc(sizeof(CFIndex) * urlStringRange.length);
                    CTRunGetStringIndices(run, urlStringRange, buffer);
                    // TODO: Map the glyph indices to indexes in the string & Fire the delegate
                }
            }
        }
    }
}

这不是目前最漂亮的代码,我仍在努力让它工作,所以请原谅代码质量。

我遇到的问题是,当我点击链接外部时,我预期会发生的事情发生了:什么都没有被触发。

但是,如果我点击链接所在的同一行,我希望 "Tapped line" 得到打印,但这并没有发生,我希望 "Tapped line ""Taped run" 在我点击 URL 时打印。

我不确定在哪里进一步解决这个问题,我为解决这个问题而查看的资源是特定于 Cocoa 的(几乎完全不适用),或者缺乏关于这个特定案例的信息。

我很乐意向文档提供指导,这些文档详细说明了如何正确地检测触摸是否发生在通过代码绘制的核心文本的边界内,但此时,我只想解决这个问题,所以任何帮助将不胜感激。

更新:我已将我的问题缩小为坐标问题。我翻转了坐标(而不是如上所示),我遇到的问题是触摸记录如我所料,但坐标空间翻转了,我似乎无法将其翻转回来。

最佳答案

我刚刚这样做是为了从触摸位置获取字符串字符索引。在这种情况下,行号将是 i:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touch ended");

    UITouch* touch = [touches anyObject];

    CGPoint pnt = [touch locationInView:self];

    CGPoint reversePoint = CGPointMake(pnt.x, self.frame.size.height-pnt.y);

    CFArrayRef lines = CTFrameGetLines(ctFrame);

    CGPoint* lineOrigins = malloc(sizeof(CGPoint)*CFArrayGetCount(lines));

    CTFrameGetLineOrigins(ctFrame, CFRangeMake(0,0), lineOrigins);

    for(CFIndex i = 0; i < CFArrayGetCount(lines); i++)
    {
        CTLineRef line = CFArrayGetValueAtIndex(lines, i);

        CGPoint origin = lineOrigins[i];
        if (reversePoint.y > origin.y) {
            NSInteger index = CTLineGetStringIndexForPosition(line, reversePoint);
            NSLog(@"index %d", index);
            break;
        }
    }
    free(lineOrigins);
}

关于iphone - 使用 CoreText 和 touches 创建可点击的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3802697/

相关文章:

ios - 如何在 iPad SplitViewController 中使用 SWRevealViewController

iphone - 设备旋转时更改 UIWindow 背景颜色

iphone - 如果用户在 iPhone 和 window.navigator.standalone == true 上,则在 userAgent 时更改 css 显示

ios - UICollectionView 页脚未显示

iphone - Objective-C 拆分()?

objective-c - 获取 OSX 连接的 Wi-Fi 网络名称

iphone - UIView 不透明属性

ios - 如何使用一个 UIDatePicker 在单击一个按钮时仅显示时间,在另一个按钮单击时仅显示日期?

iphone - 添加 CHCSVPARSER 时出现错误

iphone - EKEventStore.calendars 返回不存在的 "calendar"对象