CoreText绘图,接收触摸,触摸坐标混乱
我有一个使用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;
}
}
}
}
}
这适用于第一行,例如,我正在做一些数据检测的网址。 我点击它,而移动Safari则按照我的预期启动。 但是,这只有在物品位于第一行时才有效。 我在其他行上看到文字着色和加下划线(用于地址和电话号码等内容),并且我已验证相应的数据已附加到字符串中的自定义属性中,但是当我点击它们时,什么都不会发生。
我知道我不得不以某种方式翻转lineBounds
,但我不知道如何去做。 任何帮助将不胜感激。
实际上,你需要做的是反向遍历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];
最简单的方法来扭转线条
CFArrayRef lines = (CFArrayRef)[[(NSArray *)CTFrameGetLines(frame) reverseObjectEnumerator] allObjects];
链接地址: http://www.djcxy.com/p/81983.html
上一篇: CoreText drawing, receiving touches, touch coordinates messed up
下一篇: Using CoreText and touches to create a clickable action