6

我正在使用drawpdf函数在ios应用程序中生成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);
}

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

我用了

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

CGContextSetFontSize ( currentContext, 20);

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

我在 uiwebview 中显示生成的 pdf。

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

4

6 回答 6

6

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

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

将此添加到CFAttributtedStringRef指针。

这应该有效。

于 2013-01-30T11:33:53.977 回答
2

我想这些就是你要找的

void CGContextSelectFont (
    CGContextRef c,
    const char *name,
    CGFloat size,
    CGTextEncoding textEncoding
);

void CGContextSetFontSize (
   CGContextRef c,
   CGFloat size
);

就像是...

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

或者

CGContextSetFontSize ( currentContext, 20);

希望对您有所帮助,这里有更多... Quartz 2D Programming Guide - Text

于 2013-01-21T15:57:06.423 回答
2

创建CTFont使用CTFontCreateWithNameAndOptions(检查:CTFont 参考

创建一个可变属性字符串(CFMutableAttributedString 参考)您可以编辑此可变字符串并进行所需的任何更改。在你的情况下,设置它的字体。

用这个字符串创建你的 CTFrameSetter,字体应该会出现。

如果您想彻底并有足够的时间,请阅读字符串编程指南

如果您想要一个使用 CFMutableAttributedString 的快速代码示例,请在 stackoverflow 上搜索 CFMutableAttributedString,大多数答案都会让您了解如何使用它。例如这个问题

于 2013-01-24T11:39:13.933 回答
1

您可以尝试“libHaru”在 iPhone 项目中生成 pdf。

https://github.com/akisute/iPhonePDF

希望能帮助到你 !!!

于 2013-01-30T10:12:23.363 回答
1

怎么样CGContextSetFont

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

不要忘记释放它:

CGFontRelease(fontRef);
于 2013-01-30T16:23:17.290 回答
0

经过大量搜索,发现 itsdamslife 的答案是正确的,但它不支持我的代码。

但是,我很便宜地解决了它。即,多次调用该drawText:函数以获取粗体。

for(int i=0;i<5;i++)
{
 [self drawtext];
} 
于 2013-02-18T09:39:15.617 回答