iOS:如何在pdf生成中绘制文本时设置字体?

Azi*_*lah 6 iphone pdf-generation objective-c font-size ios

我在ios应用程序中使用drawpdf函数生成pdf,同时在nsobject类中调用drawtext函数,它根据我指定的框架和字符串清楚地绘制文本.

我的代码是

+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect
{

    CFStringRef stringRef = (__bridge CFStringRef)textToDraw;
// Prepare the text using a Core Text Framesetter
    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);
}
Run Code Online (Sandbox Code Playgroud)

但是我需要将文本fontsize设置得更大更大胆.

我用了

CGContextSelectFont(currentContext, "Helvetica", 20, kCGEncodingMacRoman);
Run Code Online (Sandbox Code Playgroud)

CGContextSetFontSize ( currentContext, 20);
Run Code Online (Sandbox Code Playgroud)

参考上述函数中mr.texian提供的答案,但没有变化

我在uiwebview中显示生成的pdf.

我从网上得到了这个代码.我不知道在哪里编辑字体设置的代码.任何帮助将不胜感激.

its*_*ife 6

请尝试CoreText,一个非常有用的基础类.以下是示例代码:

CTFontRef font = CTFontCreateWithName((CFStringRef)@"Arial", 16.0f, nil);
CFAttributedStringSetAttribute(textString,CFRangeMake(0, strLength-1), kCTFontAttributeName, font);
Run Code Online (Sandbox Code Playgroud)

将其添加到CFAttributtedStringRef指针.

这应该工作.