Генерация динамического PDF для iOS с желаемым типом шрифта и цветом шрифта

Я создаю динамический PDF из своего приложения. В некоторых случаях я хочу, чтобы мой текст был написан в формате PDF с желаемым цветом. как я могу это получить?

Я использую CoreText.

Вот мой код для рисования текста в моем PDF,

+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect
{
    frameRect.origin.y = frameRect.origin.y + frameRect.size.height; // New line
    CFStringRef stringRef = ( CFStringRef)textToDraw;
    CGColorSpaceCreateWithName(stringRef);
    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
    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, frameRect.origin.y*2);
    CGContextScaleCTM(currentContext, 1.0, -1.0);
    // Draw the frame.
    CTFrameDraw(frameRef, currentContext);
    CGContextScaleCTM(currentContext, 1.0, -1.0);
    CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);
    CFRelease(frameRef);
    //CFRelease(stringRef);
    CFRelease(framesetter);
}

Будем признательны за любую помощь или предложение. Заранее спасибо.


person Dhruvik    schedule 10.05.2013    source источник


Ответы (1)


Если вы используете coretext, попробуйте

CTFontRef font = CTFontCreateWithName((CFStringRef)@"Helvetica", 16.0f, nil);
CFAttributedStringSetAttribute(textString,CFRangeMake(0, strLength-1), kCTFontAttributeName, font);

Вы также можете попробовать CGContextSetFont

NSString *fontName = @"Helvetica";
CGFontRef fontRef = CGFontCreateWithFontName((__bridge CFStringRef)fontName);
CGContextSetFont(context, fontRef);
CGContextSetFontSize(context, 30);

См. мой вопрос, вы получить идею.

person Nazik    schedule 10.05.2013