UIWebView
只是为了得到可点击的链接。一旦用户点击该链接(而不是整个表视图单元格),我想启动一个委托方法,然后用它来呈现一个模式视图,其中包含指向该url的web视图。
我将路径和字符串本身保存为视图的实例变量,绘图代码在
-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 run"
如果我点击网址就会被打印出来。
更新
:我已将问题缩小为协调问题。我已经翻转了坐标(并没有如上所示),我得到的问题是,我接触寄存器,因为我期望,但坐标空间是翻转,我似乎不能翻转回来。