【发布时间】:2015-04-27 15:18:49
【问题描述】:
我正在尝试创建可在 iOS 设备(仅限 iPad)上运行的 PDF。我已经创建了我的 PDF,并且可以在其中写入文本和图形。但是我很难写旋转的文本,这样文本会旋转,但原点会保持不变。这是我试图实现的输出图像:
我这样创建我的 PDF:
-(void)drawPDF:(NSString*)fileName
{
self.pageSize = CGSizeMake(792, 612);
// Create the PDF context using the default page size of 612 x 792.
UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
// Get the graphics context.
context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, 654);
CGContextScaleCTM(context, 1.0, -1.0);
UIFont *font = [UIFont fontWithName:@"Helvetica-Light" size:8];
[self drawText:@"here is a really long line of text so we can see" withFrame:CGRectMake(400, 100, 50, 50) withFont:font rotation:-50];
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}
然后我有这个绘制文本的功能,我尝试了很多变体:
-(void)drawText:(NSString *)text withFrame:(CGRect)rect withFont:(UIFont *)font rotation:(float)degrees
{
float radians = [PDFMaker DegreesToRadians:degrees];
NSDictionary *attribs = @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Light" size:10.0]};
CGContextSaveGState(context);
CGPoint pos = CGPointMake(rect.origin.x, -50 - rect.origin.y);
// Translate so we're drawing in the right coordinate space
CGContextScaleCTM(context, 1.0, -1.0);
CGSize stringSize = [text sizeWithAttributes:attribs];
CGContextTranslateCTM(context, 0-stringSize.width/2, 0-stringSize.height/2);
CGContextRotateCTM(context, radians);
CGContextTranslateCTM(context, stringSize.width/2, stringSize.height/2);
[text drawAtPoint:CGPointMake(pos.x, pos.y) withAttributes:attribs];
CGContextRestoreGState(context);
}
但无论我如何使用翻译,文本都会跳来跳去,就好像围绕原点的轴旋转一样。我已经阅读了其他答案,以在原点的中心旋转文本并尝试过,但这似乎不起作用(或者,可能更准确地说,我无法让它工作)。我怀疑问题可能与使用 PDF 上下文有关,但真的不知道。任何帮助将不胜感激。
【问题讨论】:
标签: ios objective-c pdf-generation quartz-2d