ios 代码生成绘制 pdf 小技巧

  绘制的整个过程      

  UIGraphicsBeginPDFPage();//创建一个空白页

   CGContextSetTextMatrix(pdfContext, flip);//为上下文设置仿射矩阵

        CGContextBeginPath(pdfContext); // 打开上下文 开始绘制

 

CGFloat height=50;

 NSString*  textContent=[NSStringstringWithFormat:@"绘制第一个PDF"];

NSString *  width=[self getfontwidth:textContent fontsize:20];

  int     font=[UIFontfontWithName:@"Arial-BoldMT"size:20];

[textContent drawAtPoint:CGPointMake((612-width)/2, height) withFont:font];

 

//获得字符串宽度

-(CGFloat)getfont(NSString *)string fontsize:(int)fontsize{

    UIFont *font=[UIFontsystemFontOfSize:fontsize];

    CGSize size=[string sizeWithFont:font];

    return size.width;

}

1. 另起一页

 UIGraphicsBeginPDFPage(); // 就ok了

 2. 绘制一条线

        //set the path of the line that we will draw

        //起始点

         CGContextMoveToPoint(pdfContext, 50, height);//当前上下文移动到固定的一点(起始点)

        //结束点

                    CGContextAddLineToPoint(pdfContext, 612-50, height);// 在上下当前图层上下文的坐标点绘制一个直线结束点)

                   //绘制 线

        CGContextStrokePath(pdfContext);

原文地址:https://www.cnblogs.com/zander/p/2670045.html