gpt4 book ai didi

iphone - CoreText 绘图、接收触摸、触摸坐标困惑

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:28:02 26 4
gpt4 key购买 nike

我有一个使用 coretext 绘制一些文本的 View 。一切正常,文本显示在右侧,翻转坐标。但是,我还必须响应特定运行时的触摸,因此我有以下代码,当点击手势识别器触发时会调用它:

- (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;
}
}
}
}
}

这适用于第一行,例如,我正在对其进行一些数据检测的网址。我点击它,然后 Mobile Safari 如我所料启动。但是,这仅在项目位于第一行时才有效。我在其他行看到带有颜色和下划线的文本(例如地址和电话号码),并且我已验证适当的数据已附加到字符串中的自定义属性,但是当我点击它们时,没有任何反应。

我知道我必须以某种方式翻转矩形 lineBounds,但我不确定该怎么做。任何帮助将不胜感激。

最佳答案

实际上您需要做的是反向迭代。例如,尝试替换为以下代码:

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];

关于iphone - CoreText 绘图、接收触摸、触摸坐标困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4572485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com