Я использую Core Text, в частности CTFramesetter, чтобы создать красивый текстовый макет в PDF. Все работает нормально, но при первом вызове я получаю следующее сообщение об ошибке на консоли:
<Error>: unsupported 'Zapf' version 00020000.
Нет ошибок при последующем вызове.
Ошибка возникает, когда приписываемая строка содержит символ новой строки (\ n), но не когда его нет.
Код, который воспроизводит проблему ниже. Я разместил это свежий проект xcode, просто добавив инфраструктуру Core Text для его компиляции, поэтому я вполне уверен, что проблема кроется здесь.
Кто-нибудь знает, что означает ошибка и как ее избежать?
Как уже отмечалось, PDF-файл, который он создает, просто отлично. Хотя приведенный ниже код ничего не делает, в моем приложении я использую центрирование, отступ, переменный межстрочный интервал и т. Д. Поэтому обойти проблему, скажем, drawInRect: withFont на самом деле не вариант. (Но если я использую drawInRect: withFont: в этом контексте, определенно без ошибок.)
- (void) reproduceTheError;
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* filename = [[paths objectAtIndex:0] stringByAppendingPathComponent: @"testPFF.PDF"];
CGRect paperRect = CGRectMake(0.0, 0.0, 595.44, 841.68);
UIGraphicsBeginPDFContextToFile(filename, paperRect, nil);
UIGraphicsBeginPDFPage();
// gives a '<Error>: unsupported 'Zapf' version 00020000.' on first call (but not later calls)
// if the \n is removed from the string, no error
NSMutableAttributedString* string = [[NSMutableAttributedString alloc] initWithString: @"Hello World. ‘Smart quotes’ \nWhat's up?"];
[string autorelease];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef) string);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSaveGState (currentContext);
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, CGRectInset(paperRect, 100.0, 100.0));
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), framePath, NULL);
CGPathRelease(framePath);
// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, paperRect.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
CFRelease(frameRef);
CFRelease(framesetter);
CGContextRestoreGState (currentContext);
UIGraphicsEndPDFContext();
}