0

我正在使用 NSUserDefaults 将输入的文本保存到多个 UITextField 中。我想在 PDF 上显示文本。我可以使用以下方法处理每个字段并将其添加到 PDF 中吗?现在在我的示例中,您可以看到我是如何添加“名称”字段的。任何提示表示赞赏!

 +(void)drawText
{
   NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
   NSString * nameString = [prefs stringForKey:@"name"];

   CFStringRef stringRef = (CFStringRef)nameString;

   // Prepare the text using a Core Text Framesetter
   CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
   CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);

   CGRect frameRect = CGRectMake(30, 0, 300, 60);
   CGMutablePathRef framePath = CGPathCreateMutable();
   CGPathAddRect(framePath, NULL, frameRect);

   // Get the frame that will do the rendering.
   CFRange currentRange = CFRangeMake(0, 0);
   CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
   CGPathRelease(framePath);

   // Get the graphics context.
   CGContextRef currentContext = UIGraphicsGetCurrentContext();

   // Put the text matrix into a known state. This ensures
   // that no old scaling factors are left in place.
   CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

   // Core Text draws from the bottom-left corner up, so flip
   // the current transform prior to drawing.
   CGContextTranslateCTM(currentContext, 0, 100);
   CGContextScaleCTM(currentContext, 1.0, -1.0);

   // Draw the frame.
   CTFrameDraw(frameRef, currentContext);

   CFRelease(frameRef);
   CFRelease(stringRef);
   CFRelease(framesetter);
}
4

1 回答 1

0

为 textField 中的动态文本更改如下所示的方法:

+(void)drawText:(NSString *)value index:(int)index
{
   //NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
   //NSString * nameString = [prefs stringForKey:@"name"];

   CFStringRef stringRef = value; //(CFStringRef)nameString;

   // Prepare the text using a Core Text Framesetter
   CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
   CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);

   CGRect frameRect = CGRectMake(30, (index*70), 300, 60); //for horizontal textfields
   CGMutablePathRef framePath = CGPathCreateMutable();
   CGPathAddRect(framePath, NULL, frameRect);

   // Get the frame that will do the rendering.
   CFRange currentRange = CFRangeMake(0, 0);
   CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
   CGPathRelease(framePath);

   // Get the graphics context.
   CGContextRef currentContext = UIGraphicsGetCurrentContext();

   // Put the text matrix into a known state. This ensures
   // that no old scaling factors are left in place.
   CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

   // Core Text draws from the bottom-left corner up, so flip
   // the current transform prior to drawing.
   CGContextTranslateCTM(currentContext, 0, 100);
   CGContextScaleCTM(currentContext, 1.0, -1.0);

   // Draw the frame.
   CTFrameDraw(frameRef, currentContext);

   CFRelease(frameRef);
   CFRelease(stringRef);
   CFRelease(framesetter);
}

像这样使用,因为我假设你有用于添加文本字段的值数组:

for(int i=0;i>[arrOfValues count];i++)
{
   [YourControllerName drawText:[arrOfValues objectAtIndex:i] index:i];
}
于 2012-09-03T05:10:29.647 回答