嗯,这个是左上角为0,0点,和上次写的那个不支持中文得不一样,那个事左下角为0,0点
-(int)plaintextatwith:(int)beginx width:(int)width text:(NSString *)text height:(int)height fontsize:(int)fontsize context:(CGContextRef)context{
int linewidth=width;
int i=0,j=0;
// const char *texts;
NSString *thisline;
for(;i<text.length;){
thisline=@"";
//剩余字符长度大于linewidth
if([[text substringFromIndex:j] sizeWithFont:[UIFont fontWithName:@"Times New Roman" size:fontsize]].width>linewidth){
while ([thisline sizeWithFont:[UIFont fontWithName:@"Times New Roman" size:fontsize]].width<linewidth) {
i++;
thisline=[text substringWithRange:NSMakeRange(j, i-j)];
}
if(j!=0){
height=height+fontsize+3;
}
j=i;
NSString *linetext=[NSString stringWithFormat:@"%@",thisline];
NSRange range=[linetext rangeOfString:@"\n"];
while (range.length>0) {
NSString *linetext1=[NSString stringWithFormat:@"%@",[linetext substringToIndex:range.location]];
[linetext1 drawAtPoint:CGPointMake(beginx, height) withFont:[UIFont systemFontOfSize:fontsize]];
height=height+fontsize+3;
linetext=[linetext substringFromIndex:range.location+range.length];
range=[linetext rangeOfString:@"\n"];
}
[linetext drawAtPoint:CGPointMake(beginx, height) withFont:[UIFont systemFontOfSize:fontsize]];
//换页方法
if(height>700){
UIGraphicsBeginPDFPage();
height=50;
CGContextSelectFont(context, "Arial", fontsize, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 0, 0, 0, 1);
}
//剩余字符小于linewidth时
}else{
thisline=[text substringFromIndex:j];
i=text.length;
if(j!=0){
height=height+fontsize+3;
}
NSString *linetext=[NSString stringWithFormat:@"%@",thisline];
NSRange range=[linetext rangeOfString:@"\n"];
while (range.length>0) {
NSString *linetext1=[NSString stringWithFormat:@"%@",[linetext substringToIndex:range.location]];
[linetext1 drawAtPoint:CGPointMake(beginx, height) withFont:[UIFont systemFontOfSize:fontsize]];
height=height+fontsize+3;
linetext=[linetext substringFromIndex:range.location+range.length];
range=[linetext rangeOfString:@"\n"];
}
[linetext drawAtPoint:CGPointMake(beginx, height) withFont:[UIFont systemFontOfSize:fontsize]];
if(height>700){
UIGraphicsBeginPDFPage();
height=50;
CGContextSelectFont(context, "Arial", fontsize, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 0, 0, 0, 1);
}
}
}
return height;
}