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

标签 iphone ios objective-c pdf-generation font-size

我在 ios 应用程序中生成一个 pdf,使用 drawpdf 函数,同时调用 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);
}

但我需要将文本字体大小设置得更大更粗。

我用过

CGContextSelectFont(currentContext, "Helvetica", 20, kCGEncodingMacRoman);

CGContextSetFontSize ( currentContext, 20);

引用mr.texian在上述函数中提供的答案,但没有变化

我在 uiwebview 中显示生成的 pdf。

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

最佳答案

请尝试 CoreText,这是一个非常有用的基础类(class)。以下是示例代码:

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

将其添加到 CFAttributtedStringRef 指针。

这应该有效。

关于iphone - iOS:在pdf生成中绘制文本时如何设置字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14441534/

相关文章:

ios - NSLayoutConstraint 和 "attach"两个标签互相

iphone - 使用 iOS 在 cordova 项目中打开本地 pdf

iphone - subview 中的 TabBarController

ios - 快速单击按钮时显示小数

objective-c - 使用自己用 objective-c 编写的应用程序打开文件

objective-c - 如何在 objective-c 中将html数据转换为PDF

iphone - AGImagePickerController 难度

ios - Xcode 7 不显示启动图像

ios - 添加本地化时的 Xcode 未找到所有 Storyboard

objective-c - Objective C 如果成功就简单