簡體   English   中英

CoreText繪圖,接收觸摸,觸摸坐標搞砸了

[英]CoreText drawing, receiving touches, touch coordinates messed up

我有一個使用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;
                }
            }
        }
    }
}

這適用於第一行,例如,我正在進行一些數據檢測的網址。 我輕拍它,Mobile Safari就像我期望的那樣啟動。 但是,這僅在項目位於第一行時才有效。 我在其他行上看到文字的顏色和下划線(用於地址和電話號碼等內容),我已經驗證了相應的數據附加到字符串中的自定義屬性,但是當我點擊它們時,什么也沒發生。

我知道我必須以某種方式翻轉rect 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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM