如何在 iOS 上使用 CoreGraphics 渲染的 pdf 上反转文本颜色?

Mok*_*gra 2 pdf text core-graphics quartz-2d ios

我正在使用开源 PDF 查看库(VFR PDF 阅读器https://github.com/vfr/Reader)。我正在尝试使用白色文本实现“夜间模式”或黑色背景。我可以将背景设置为我喜欢的任何颜色,但无法更改文本颜色。您可以在https://github.com/vfr/Reader/blob/master/Sources/ReaderContentPage.m的“drawLayer”方法中查看可以在哪里修改背景颜色。它只是更改渲染 PDF 的矩形的颜色。

我的问题是:我可以对“上下文”做些什么来导致 pdf 中的文本改变颜色(在我的情况下我想要白色文本)?有问题的行看起来像这样(第 558 行):

CGContextDrawPDFPage(context, _PDFPageRef); // Render the PDF page into the context
Run Code Online (Sandbox Code Playgroud)

小智 6

这是我解决夜视的方法:

1)我在 NSUserDefault 中跟踪用户偏好(夜间模式或白天模式)

2) 在

- (void)drawLayer:(CATiledLayer *)layer inContext:(CGContextRef)context
{
Run Code Online (Sandbox Code Playgroud)

a) 检索用户偏好

NSString *userViewMode = [[NSUserDefaults standardUserDefaults] objectForKey:@"DayOrNightSetting"];
Run Code Online (Sandbox Code Playgroud)

b) 将 CGContextRef 的背景颜色设置为白色

 CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
Run Code Online (Sandbox Code Playgroud)

c) 将 CGContextDrawPage(Context,_PDFPageRef) 替换为:

if ([userViewMode isEqualToString:@"Night"]) {
        CGContextSetBlendMode(context, kCGBlendModeDestinationAtop);

        CGContextDrawPDFPage(context, _PDFPageRef);
        CGContextSetBlendMode(context, kCGBlendModeExclusion);
        CGContextDrawPDFPage(context, _PDFPageRef);
    }
    else
    {
       CGContextSetBlendMode(context,kCGBlendModeNormal);
    CGContextDrawPDFPage(context, _PDFPageRef);
    }
Run Code Online (Sandbox Code Playgroud)