Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS动态PDF生成,具有所需的文本字体类型和字体颜色_Ios_Ipad_Pdf_Core Text - Fatal编程技术网

iOS动态PDF生成,具有所需的文本字体类型和字体颜色

iOS动态PDF生成,具有所需的文本字体类型和字体颜色,ios,ipad,pdf,core-text,Ios,Ipad,Pdf,Core Text,我正在从我的应用程序创建动态PDF。在某些情况下,我希望我的文本以所需颜色的PDF格式书写。我怎么能得到这个 我正在使用CoreText 这是我在PDF中绘制文本的代码 +(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect { frameRect.origin.y = frameRect.origin.y + frameRect.size.height; // New line CFStringRef st

我正在从我的应用程序创建动态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);
}
任何帮助或建议都将不胜感激。,
提前感谢。

如果您使用的是
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);
看,你会有一个想法。

看这个链接