2012-04-12 128 views
2

雖然我認爲這是一個基本問題,但我沒有設法找到可用的響應。我通過將路徑拖動到PDF上下文來創建PDF文件,並且我希望圖形上的不同區域可以超鏈接到外部內容(http://bla.bla)。即使區域是不相交的矩形,我也會很開心。任何人都知道如何做到這一點?以編程方式在PDF文件中創建鏈接

回答

3

檢查這個問題的答案它的工作原理:Embed hyperlink in PDF using Core Graphics on iOS

- (void) drawTextLink:(NSString *) text inFrame:(CGRect) frameRect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGAffineTransform ctm = CGContextGetCTM(context); 

    // Translate the origin to the bottom left. 
    // Notice that 842 is the size of the PDF page. 
    CGAffineTransformTranslate(ctm, 0.0, 842); 

    // Flip the handedness of the coordinate system back to right handed. 
    CGAffineTransformScale(ctm, 1.0, -1.0); 

    // Convert the update rectangle to the new coordiante system. 
    CGRect xformRect = CGRectApplyAffineTransform(frameRect, ctm); 

    NSURL *url = [NSURL URLWithString:text];   
    UIGraphicsSetPDFContextURLForRect(url, xformRect); 

    CGContextSaveGState(context); 
    NSDictionary *attributesDict; 
    NSMutableAttributedString *attString; 

    NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle]; 
    attributesDict = @{NSUnderlineStyleAttributeName : underline, NSForegroundColorAttributeName : [UIColor blueColor]}; 
    attString = [[NSMutableAttributedString alloc] initWithString:url.absoluteString attributes:attributesDict]; 

    [attString drawInRect:frameRect]; 

    CGContextRestoreGState(context); 
} 
+0

Thanks!8morechr – user1258240 2014-04-12 13:11:03

+2

如何通過frameRect方法drawTextLink方法? – AppAspect 2014-10-29 12:22:05