2

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!

Community
  • 1
  • 1
matrix4use
  • 611
  • 1
  • 6
  • 20
  • 2
    This is a bug in iOS 7. I ran into the same problem. If the attributed string has any underlined text, it won't appear in the PDF. I submitted a bug report 2 months ago. – rmaddy Jan 14 '14 at 01:48
  • 2
    My workaround was to remove any underline so at leat the rest of the text appears. – rmaddy Jan 14 '14 at 01:51
  • OK, @rmaddy I submitted a report too. – matrix4use Jan 14 '14 at 02:23

1 Answers1

2

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];
}
Community
  • 1
  • 1
matrix4use
  • 611
  • 1
  • 6
  • 20