IOS: How to set the font when drawing pdf text?

I create a pdf file in an ios application using the drawpdf function, calling the drawtext function in the nsobject class, it explicitly draws the text according to the frame and line that I specified.

My code

+(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);
}

But I need to set the font of the font larger and bolder.

I used

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

and

CGContextSetFontSize ( currentContext, 20);

with reference to the answer provided by mr.texian in the above function, but unchanged

I am showing the generated pdf file in uiwebview.

I got this code from the network. I did not know where to edit the code to customize the font. Any help would be appreciated.

+5
source share
6 answers

Try CoreText, a very useful foundation class. The following is sample code:

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

Add this to the index CFAttributtedStringRef.

.

+6

, ,

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);

, , ... 2D- -

+2

CTFont CTFontCreateWithNameAndOptions (: CTFont)

( CFMutableAttributedString) . .

CTFrameSetter , .

,

CFMutableAttributedString, CFMutableAttributedString stackoverflow, , . .

+2

"libHaru" pdf iPhone.

https://github.com/akisute/iPhonePDF

, !!!

+1

CGContextSetFont?

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

:

CGFontRelease(fontRef);
+1

, fnd, , .

. .. drawText:, .

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

All Articles