NSAttributedString does not appear when generating PDF

别等时光非礼了梦想. 提交于 2019-12-11 03:53:23

问题


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

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];
}

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...

Please help!


回答1:


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];
}


来源:https://stackoverflow.com/questions/21104123/nsattributedstring-does-not-appear-when-generating-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!