我正在使用CoreText来布置自定义视图。对我来说,下一步是检测触摸事件/手势事件上的哪个单词被点击。我已经完成了这方面的研究,并发现如何自定义标签的URL来接收触摸 - 但没有通用的建议。有没有人知道如何做到这一点?如何检测使用CoreText触摸哪个单词
UPDATE:
这是我的drawRect内的代码:方法 self.attribString = [ASTRING拷贝];
3210这里就是我试图处理触摸事件
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
CGContextRef context = UIGraphicsGetCurrentContext();
CFArrayRef lines = CTFrameGetLines(frameRef);
for(CFIndex i = 0; i < CFArrayGetCount(lines); i++)
{
CTLineRef line = CFArrayGetValueAtIndex(lines, i);
CGRect lineBounds = CTLineGetImageBounds(line, context);
NSLog(@"Line %ld (%f, %f, %f, %f)", i, lineBounds.origin.x, lineBounds.origin.y, lineBounds.size.width, lineBounds.size.height);
NSLog(@"Point (%f, %f)", point.x, point.y);
if(CGRectContainsPoint(lineBounds, point))
{
似乎CTLineGetImageBounds返回一个错误的起点(大小似乎是正确的),这里是NSLog的“0号线的一个例子(0.562500,-0.281250,279.837891,17.753906)“。
我应该补充说,所有的来源在价值上都非常相似。 – xizor 2012-03-12 15:03:43