IOS Dynamic PDF, with the desired text font type and font color

I am creating a dynamic PDF file from my application. In some cases, I want my text to be written in PDF format with the desired color. how can i get this?

I am using CoreText.

Here is my code for drawing text in my PDF file,

+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect { frameRect.origin.y = frameRect.origin.y + frameRect.size.height; // New line CFStringRef stringRef = ( CFStringRef)textToDraw; CGColorSpaceCreateWithName(stringRef); 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); } 

Any help or suggestion would be appreciated. Thanks in advance.

+4
source share
1 answer

If you are using coretext try

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

You can also try CGContextSetFont

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

Look at my question , you get an idea.

0
source

Source: https://habr.com/ru/post/1480089/


All Articles