0

複数の UITextFields に入力されたテキストを保存するために NSUserDefaults を使用しています。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 に答える