生成PDF时未出现NSAttributedString [英] NSAttributedString does not appear when generating PDF

查看:93
本文介绍了生成PDF时未出现NSAttributedString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这曾经可以工作,但是现在突然停止了工作:

This used to work and now, all of a sudden, it stopped working:

对于iOS 7 iPad App,我使用以下方式生成PDF

For an iOS 7 iPad App, I generate a PDF with

UIGraphicsBeginPDFContextToFile(pdfPathWithFileName, CGRectZero, nil);
...
UIGraphicsEndPDFContext();

在该代码块内,以下方法用于对文本加下划线,但最近,它刚刚停止工作,并且传递给该方法的任何文本都完全不被渲染:

Within that code block, the following methods used to render text underlined, but recently, it just stopped working and any text passed to the method is not rendered at all:

+(void)drawUnderlinedText:(NSString *)text withFont:(UIFont *)font andColor:(UIColor *)color andLocationX:(int)locationX andLocationY:(int)locationY andTextAreaWidth:(int)textWidth andTextAreaHeight:(int)textHeight{

    NSDictionary *attributesDict;
    NSMutableAttributedString *attString;

    attributesDict = @{NSForegroundColorAttributeName : color, NSFontAttributeName : font, NSUnderlineStyleAttributeName : [NSNumber numberWithInt:NSUnderlineStyleSingle]};
    attString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributesDict];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, color.CGColor);
    CGRect rect = CGRectMake(locationX, locationY, textWidth, textHeight);
    [attString drawInRect:rect];
}

关于在Web上绘制PDF上下文,我找不到任何东西.有帖子(

I can't find anything on the web with regards to drawing to the PDF context. There are posts (and here) that mentions that issue with regards to labels. But there doesn't seem to be a solution for my problem when generating PDF files...

请帮助!

推荐答案

由于这是iOS中的错误,因此我决定使用

Since this is a bug in iOS I decided to use the work-around from here by just drawing a line to the context (see comments in code):

+(void)drawUnderlinedText:(NSString *)text withFont:(UIFont *)font andColor:(UIColor *)color andLocationX:(int)locationX andLocationY:(int)locationY andTextAreaWidth:(int)textWidth andTextAreaHeight:(int)textHeight{

    NSDictionary *attributesDict;
    NSMutableAttributedString *attString;

    // Commented out until iOS bug is resolved:    
    //attributesDict = @{NSUnderlineStyleAttributeName : [NSNumber numberWithInt:NSUnderlineStyleSingle], NSForegroundColorAttributeName : color, NSFontAttributeName : font};
    attributesDict = @{NSForegroundColorAttributeName : color, NSFontAttributeName : font};
    attString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributesDict];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, color.CGColor);
    CGRect rect = CGRectMake(locationX, locationY, textWidth, textHeight);

    // Temporary Solution to NSUnderlineStyleAttributeName - Bug:
    CGContextSetStrokeColorWithColor(context, color.CGColor);
    CGContextSetLineWidth(context, 1.0f);

    CGSize tmpSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(200, 9999)];

    CGContextMoveToPoint(context, locationX, locationY + tmpSize.height - 1);
    CGContextAddLineToPoint(context, locationX + tmpSize.width, locationY + tmpSize.height - 1);

    CGContextStrokePath(context);
    // End Temporary Solution

    [attString drawInRect:rect];
}

这篇关于生成PDF时未出现NSAttributedString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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