问题描述
这曾经可以工作,但是现在突然停止了工作:
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上下文,我找不到任何东西.有帖子()提到了有关标签的问题.但是在生成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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!