在 iOS 上使用 Core Graphics 在 PDF 中嵌入超链接 [英] Embed hyperlink in PDF using Core Graphics on iOS

查看:43
本文介绍了在 iOS 上使用 Core Graphics 在 PDF 中嵌入超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一件非常简单的事情:在 PDF 文件中写入一个用户可以实际点击的 URL.

I'm trying to do a quite simple thing: write an URL inside a PDF file that can be actually clicked by the user.

我确信使用 libharu 可以做到.我正在寻找的是使用 Core Graphics 做同样的事情,因为我的应用程序中已有的整个代码已经在使用这些方法.

I know for sure that using libharu it can be done. What I'm looking for is to do the same using Core Graphics since the whole code I already have in my app is already using those methods.

== 编辑 ==

我想我找到了一些东西:UIGraphicsSetPDFContextURLForRect 但我无法让它工作.

I think I found something: UIGraphicsSetPDFContextURLForRect but I can't make it to work.

我正在使用类似的东西:

I'm using something like:

NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
UIGraphicsSetPDFContextURLForRect( url, CGRectMake(0, 0, 100, 100));

虽然 rect 不可点击.

The rect is not clickable, though.

推荐答案

好的,我设法弄清楚为什么它不起作用.

Ok I managed to figure out why it wasn't working.

Core Graphics 上下文是颠倒的",原点在页面的左下角,而 UIKit 的原点在左上角.

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.

这是我想出的方法:

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

这个方法的作用是:

  • 获取当前上下文并对所提供的矩形应用转换,以便在 UIGraphicsSetPDFContextURLForRect 将其标记为可点击时获得一个在标记框时有效的矩形
  • 使用上述方法将新矩形 (xformRect) 标记为可点击
  • 保存当前上下文,以便以后执行的任何操作(颜色、大小、属性等)都不会在当前上下文中保持持久化
  • 在提供的矩形中绘制文本(现在使用 UIKit 坐标系)
  • 恢复上下文 GState
  • to get the current context and apply a transformation to the provided rect so to obtain a rect that would work when marking the box when the UIGraphicsSetPDFContextURLForRect will mark it as clickable
  • to mark the new rect (xformRect) as clickable using the aforementioned method
  • to save the current context so whatever is done later (colour, size, attributes, whatever) do not remain persistent in the current context
  • to draw the text in the provided rect (now using the UIKit coordinate system)
  • to restore the context GState

这篇关于在 iOS 上使用 Core Graphics 在 PDF 中嵌入超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆