Ok I managed to figure out why it wasn't working.
Core Graphics context are "reversed" in the sense of having the origin at the bottom left of the page while UIKit has the origin in the top-left corner.
This is the method I came up with:
- (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);
}
http://stackoverflow.com/questions/14748204/embed-hyperlink-in-pdf-using-core-graphics-on-ios
本文介绍了一种在iOS上使用Core Graphics为PDF文档添加超链接的方法。由于Core Graphics坐标系与UIKit的不同(前者原点在左下角,后者在左上角),作者详细解释了如何通过坐标转换来正确地在PDF页面中绘制带有超链接的文字。
186

被折叠的 条评论
为什么被折叠?



