我有一些在它响应于一个视图,我已经绘制在一些属性文本的抽头的触摸处理程序。通过此,我必须在那里我有一个CTRunRef(和相关联的线)的点作为以及字形的在运行的数量。

我不能够很容易地弄清楚什么是我该怎么走的字形是跑,给我的属性串,出字符串中它映射到的字符。

具体问题是我想知道用户在视图拍了拍什么词,所以我可以处理是否这个词是一个URL和断火自定义委托方法,所以我可以打开Web视图,它。我把所有的可能的子,我只是不知道如何映射在用户点击一个特定的字符串。

任何帮助,将不胜感激。

更新:其实我已经做出了这种事用不同的方式,对另一个人的断计算器的建议。基本上我所做的就是设置一个自定义属性,@"MyAppLinkAddress"的URL我的价值,当我的字符串转换为一个属性串找到。这发生之前,我画的字符串。因此,当自来水事件发生时,我刚刚检查,如果该属性存在,如果是的话,叫我的委托方法,如果没有,就忽视它。据工作如何我现在想的,但我要离开这个问题开了几天,如果有人能想出一个答案,我会愉快地接受它,如果它的工作解决方案,使一些人可以是能找到该信息在将来的某个时候是有用的。

有帮助吗?

解决方案

因此,正如我在更新中提到,我选择走不同的路线。相反,我得到了主意,使用自定义属性的属性串在指定我联系,因为我在创作时有也无妨。所以,我做到了。然后在我的触摸处理程序,当运行被窃听,我检查如果运行有属性,如果是的话,叫我代表它。从那里,我高兴地加载网页视图与URL。

修改:下面是解释我在这个答案做的代码段。享受。

// When creating the attribute on your text store. Assumes you have the URL already. 
// Filled in for convenience
NSRange urlRange = [tmpString rangeOfString:@"http://www.foo.com/"];
[self.textStore addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[UIColor blueColor].CGColor range:urlRange];
[self.textStore addAttribute:@"CustomLinkAddress" value:urlString range:urlRange];

...然后

// Touch handling code — Uses gesture recognizers, not old school touch handling.
// This is just a dump of code actually in use, read through it, ask questions if you
// don't understand it. I'll do my best to put it in context.
- (void)receivedTap:(UITapGestureRecognizer*)tapRecognizer
{
        CGPoint point = [tapRecognizer locationInView:self];

        if(CGRectContainsPoint(textRect, point))
        {
                CGContextRef context = UIGraphicsGetCurrentContext();

                point.y = CGRectGetHeight(self.contentView.bounds) - kCellNameLabelHeight - point.y;

                CFArrayRef lines = CTFrameGetLines(ctframe);
                CFIndex lineCount = CFArrayGetCount(lines);
                CGPoint origins[lineCount];
                CTFrameGetLineOrigins(ctframe, 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);
                                        NSString* urlString = [attributes objectForKey:@"CustomLinkAddress"];
                                        if(urlString && ![urlString isEqualToString:@""])
                                        {
                                                [self.delegate didReceiveURL:[NSURL URLWithString:urlString]];
                                                UIGraphicsPopContext();
                                                return;
                                        }
                                }
                        }
                }
                UIGraphicsPopContext();
        }
}

其他提示

在你找到抽头线,您可以通过调用CTLineGetStringIndexForPosition()索要指数字符串。有没有需要访问个别运行。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top