【问题标题】:iOS: How to set font when drawing text in pdf generation?iOS:如何在 pdf 生成中绘制文本时设置字体?
【发布时间】:2013-01-04 16:17:11
【问题描述】:

我正在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。

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

【问题讨论】:

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


    【解决方案1】:

    我想这些就是你要找的东西

    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

    【讨论】:

    • 是否需要使用 void CGContextSetFontSize ( CGContextRef c, CGFloat size );
    • 我对 CGContext 字体功能从来没有真正的好运气——我认为使用 Core Text 或 NSAttributedString 绘图可能更容易..
    • @nielsbot,如何使用 coretext 或 NSMutable 字符串,显示一些代码或教程链接
    • 他需要做的是将字体应用于属性字符串,正如 itsdamslife 所建议的那样。这段代码是错误的。
    【解决方案2】:

    使用CTFontCreateWithNameAndOptions 创建CTFont(检查:CTFont Reference

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

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

    如果您想彻底了解并有足够的时间,请阅读String programming Guide

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

    【讨论】:

      【解决方案3】:

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

      https://github.com/akisute/iPhonePDF

      希望对你有帮助!!!

      【讨论】:

        【解决方案4】:

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

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

        将此添加到CFAttributtedStringRef 指针。

        这应该可行。

        【讨论】:

        • 如何将上面两行代码添加到CFAttributtedStringRef指针中?
        【解决方案5】:

        CGContextSetFont 呢?

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

        别忘了释放它:

        CGFontRelease(fontRef);
        

        【讨论】:

          【解决方案6】:

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

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

          for(int i=0;i<5;i++)
          {
           [self drawtext];
          } 
          

          【讨论】:

            猜你喜欢
            • 2012-04-22
            • 1970-01-01
            • 2014-10-26
            • 2013-07-23
            • 1970-01-01
            • 2014-08-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多