【问题标题】:Changing the text and background color with Apple's PDFKit framework使用 Apple 的 PDFKit 框架更改文本和背景颜色
【发布时间】:2017-11-12 08:35:39
【问题描述】:

我想更改显示的 PDF 文档的文本和背景颜色,使用 Apple's PDFKit Framework“夜间模式”(深色背景、浅色前景,just like in Adobe Reader)。

我知道PDFPage class有一个drawWithBox:toContext:方法,可以在子类中重写来添加效果(如水印,如this WWDC 2017 session所示),但不知道怎么设置颜色属性.

有没有办法使用 PDFKit 库或 Apple 的任何其他低级 API (Quartz) 来做到这一点?

【问题讨论】:

  • 您是否在上下文中自己绘制文本?你能发布一些你在这个方法中做什么的代码吗?我看到在 PDFPage 中存在 NSAttributedString 属性字符串。
  • 我不想在页面上绘制任何新内容,我只想在夜间模式下显示现有文档(深色背景和浅色前景)。有点像 Adob​​e Reader 中的:forums.adobe.com/thread/1837487
  • 您有没有找到在您的 pdf 页面中应用夜间模式或反转颜色的方法?
  • 我们公司决定使用另一个工具包(Foxit Mobile SDK for iOS),它通过单个方法调用支持开箱即用的夜间模式。但是对于 Apple 的 SDK:不,我仍然不知道如何实现。

标签: ios objective-c swift macos pdf


【解决方案1】:

为了给pdf中的文本颜色你可以使用,

-(CGRect)addText:(NSString*)text withFrame:(CGRect)frame font:(NSString*)fontName fontSize:(float)fontSize andColor:(UIColor*)color{
    const CGFloat *val = CGColorGetComponents(color.CGColor);

    CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(currentContext, val[0], val[1], val[2], val[3]);
    UIFont *font =  [UIFont fontWithName:fontName size:fontSize];
    CGSize stringSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(pageSize.width - 2*20-2*20, pageSize.height - 2*20 - 2*20) lineBreakMode:NSLineBreakByWordWrapping];

    CGRect renderingRect = CGRectMake(frame.origin.x, frame.origin.y, textWidth, stringSize.height);

    [text drawInRect:renderingRect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];

    frame = CGRectMake(frame.origin.x, frame.origin.y, textWidth, stringSize.height);

    return frame;
}

对于背景颜色,请使用以下内容

CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(currentContext, [UIColor blueColor].CGColor );
    CGContextFillRect(currentContext, CGRectMake(0, 110.5, pageSize.width, pageSize.height));

【讨论】:

  • 该方法应该放在哪里,谁调用它?
  • 什么是pageSize?其中哪一部分应该分别用于明暗模式?
猜你喜欢
  • 2020-08-29
  • 1970-01-01
  • 2018-12-25
  • 2011-01-29
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 2013-01-15
  • 2013-05-20
相关资源
最近更新 更多