我正在使用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);
}
答案 0 :(得分: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];
}