任何人都可以帮我解决用于 A4 尺寸纸张的 UIView For Drawing pdf 的大小。
我使用的是以下尺寸,但我的 pdfView 对象(如标签、图像)不是我为绘图提供的尺寸。
我创建 .xib 并手动绘制 UILabels、图像和线条 然后使用标签属性我提供了用于绘制的框架,但它们根据 .xib 绘制不同的位置我提供了与 .xib 中相同的大小(0、0、792、 1122)。
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 792, 1122), nil);
+(void)drawLabels:(NSMutableArray*)arr isData:(BOOL)isdata
{
NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"PDFView" owner:nil options:nil];
UIView* mainView = [objects objectAtIndex:0];
if (isdata)
{
for (int i=1;i<=[arr count];i++)
{
UILabel *lbl=(UILabel*)[mainView viewWithTag:i];
[self drawText:[arr objectAtIndex:i-1] inFrame:lbl.frame isData:isdata];
}
}
}
//这是我的绘制方法
+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect isData:(BOOL)isdata
{
int length=[textToDraw length];
CFStringRef string = (__bridge CFStringRef) textToDraw;
CFMutableAttributedStringRef currentText = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CTFontRef font = CTFontCreateWithName((CFStringRef)@"Helvetica Neue Bold", 12.0f, nil);
if (isdata)
{
font = CTFontCreateWithName((CFStringRef)@"Helvetica Neue ", 12.0f, nil);
}
CFAttributedStringReplaceString (currentText,CFRangeMake(0, 0), string);
CFAttributedStringSetAttribute(currentText,CFRangeMake(0, length),kCTFontAttributeName,font);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CTFrameDraw(frameRef, currentContext);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);
CFRelease(frameRef);
CFRelease(framesetter);
}
// 对于线条图,我使用下面的代码
for (int j=1001;j<=1003;j++)
{
UILabel *lbl=(UILabel*)[mainView viewWithTag:j];
CGPoint lblPoint=CGPointMake(lbl.frame.origin.x, lbl.frame.origin.y);
CGPoint lblPoint2=CGPointMake(lbl.frame.origin.x+lbl.frame.size.width, lbl.frame.origin.y);
[self drawLineFromPoint:lblPoint toPoint:lblPoint2];
}
+(void)drawLineFromPoint:(CGPoint)from toPoint:(CGPoint)to
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.2, 0.3, 0.2, 0.4};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextMoveToPoint(context, from.x, from.y);
CGContextAddLineToPoint(context, to.x, to.y);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
}
这是我的 .xib
这是我绘制后的 .pdf 文件