使用苹果的PDFKit框架更改文本和背景颜色

14

你在上下文中自己绘制文本吗?你能贴一些你在这个方法中所做的代码吗?我看到在PDFPage中存在NSAttributedString属性字符串。 - Ramis
我不想在页面上绘制任何新内容,我只想以夜间模式(黑色背景和浅色前景)显示现有文档。就像Adobe Reader中的那样:https://forums.adobe.com/thread/1837487 - Bedford
你是否找到了在PDF页面中应用夜间模式或反转颜色的方法? - SleepNot
我们公司决定使用另一个工具包(Foxit Mobile SDK for iOS),它支持夜间模式,只需调用一个方法即可。但是对于苹果的SDK:不,我仍然不知道如何实现相同的功能。 - Bedford
1个回答

7

想要在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));

4
那个方法应该放在哪里,谁会调用它? - luk2302
什么是pageSize?在浅色/深色模式下应该使用其中的哪一部分? - user5306470

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接