Iphone 如何在iOS中以PDF格式绘制多色文本?
我正在做一个项目,我需要将一些NSString文本绘制成PDF。我能够在PDF中绘制单色文本。我想在PDF中实现搜索方法,通过它我可以搜索PDF中的特定字符串,并用不同的颜色标记它 下面是我用来在PDF中绘制单色文本的代码Iphone 如何在iOS中以PDF格式绘制多色文本?,iphone,ios,objective-c,xcode,Iphone,Ios,Objective C,Xcode,我正在做一个项目,我需要将一些NSString文本绘制成PDF。我能够在PDF中绘制单色文本。我想在PDF中实现搜索方法,通过它我可以搜索PDF中的特定字符串,并用不同的颜色标记它 下面是我用来在PDF中绘制单色文本的代码 - (void) drawText { CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(currentContext, 0.0,
- (void) drawText
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 1.0, 1.0);
NSString *textToDraw = pdfTextView.text;
UIFont *font = [UIFont systemFontOfSize:14.0];
CGSize stringSize = [textToDraw sizeWithFont:font
constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset)
lineBreakMode:UILineBreakModeWordWrap];
CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height);
[textToDraw drawInRect:renderingRect
withFont:font
lineBreakMode:UILineBreakModeWordWrap
alignment:UITextAlignmentLeft];
}
也可以从PDF检索文本(如果它只包含文本)
请告知。您需要绘制属性字符串,而不是简单字符串。试试这个
CGContextSetTextMatrix(pdfContext, CGAffineTransformIdentity);
CGMutablePathRef path = CGPathCreateMutable();
CGRect bounds = CGRectMake(x, y, width, height);
CGPathAddRect(path, NULL, bounds);
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"firstsecondthird"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(11,5)];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(string);
// Create the frame and draw it into the graphics context
CTFrameRef frame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0, 0), path, NULL);
CFRelease(framesetter);
CTFrameDraw(frame, pdfContext);
CGContextTranslateCTM(pdfContext, 0, pageheight);
CGContextScaleCTM(pdfContext, 1.0, -1.0);
这是给你指路,可能会有帮助。我使用属性字符串表示粗体和全部。希望这也适用于颜色
愉快的编码。您需要绘制属性字符串,而不是简单的字符串。试试这个
CGContextSetTextMatrix(pdfContext, CGAffineTransformIdentity);
CGMutablePathRef path = CGPathCreateMutable();
CGRect bounds = CGRectMake(x, y, width, height);
CGPathAddRect(path, NULL, bounds);
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"firstsecondthird"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(11,5)];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(string);
// Create the frame and draw it into the graphics context
CTFrameRef frame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0, 0), path, NULL);
CFRelease(framesetter);
CTFrameDraw(frame, pdfContext);
CGContextTranslateCTM(pdfContext, 0, pageheight);
CGContextScaleCTM(pdfContext, 1.0, -1.0);
这是给你指路,可能会有帮助。我使用属性字符串表示粗体和全部。希望这也适用于颜色
祝您编码愉快。谢谢您的回复。但我无法使用NSMutableAttributedString访问其他字符串方法。例如,我需要调用drawInRect方法,正如我在代码中提到的,它仅由NSString调用。请告知为什么要使用drawinrect?我想将多色字符串放入PDF。因此,上面的方法代码是将单色NSString文本绘制为PDF。感谢您的回复。但我无法使用NSMutableAttributedString访问其他字符串方法。例如,我需要调用drawInRect方法,正如我在代码中提到的,它仅由NSString调用。请告知为什么要使用drawinrect?我想将多色字符串放入PDF。因此,上面的方法代码是将单色NSString文本绘制为PDF。