Reputation: 8444
I'am generating a pdf in ios app, using drawpdf function,while calling the drawtext function in the nsobject class it draws the text clearly according to the frame and string which I specified.
My code is
+(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 text fontsize bigger 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 no changes
I am showing the generated pdf in uiwebview.
I got this code from net. I didn't know where to edit the code for font setting. Any help will be appreciated.
Upvotes: 6
Views: 4879
Reputation: 356
Please try CoreText, a very useful foundation class. Following is the sample code:
CTFontRef font = CTFontCreateWithName((CFStringRef)@"Arial", 16.0f, nil);
CFAttributedStringSetAttribute(textString,CFRangeMake(0, strLength-1), kCTFontAttributeName, font);
Add this to the CFAttributtedStringRef
pointer.
This should work.
Upvotes: 6
Reputation: 8444
After a lot search, fnd that itsdamslife's answer is correct, but it didn't support for my code.
But, I solved it cheaply.
ie, called multiple times the drawText:
function to get bold font.
for(int i=0;i<5;i++)
{
[self drawtext];
}
Upvotes: 0
Reputation: 3937
What about CGContextSetFont
?
NSString *fontName = @"Helvetica";
CGFontRef fontRef = CGFontCreateWithFontName((__bridge CFStringRef)fontName);
CGContextSetFont(context, fontRef);
CGContextSetFontSize(context, 20);
Don't forget to release it:
CGFontRelease(fontRef);
Upvotes: 1
Reputation: 1528
You can try "libHaru" for generating pdf in iPhone project.
https://github.com/akisute/iPhonePDF
Hope it Helps !!!
Upvotes: 1
Reputation: 10475
Create a CTFont
using CTFontCreateWithNameAndOptions
(check : CTFont Reference)
Create a mutable attributed string (CFMutableAttributedString Reference) You can edit this mutable string and make any changes you want. In your case, set it's font.
Create your CTFrameSetter with this string and the font should appear.
If you want to be thorough and have enough time, read the String programming Guide
If you want a quick code sample for using CFMutableAttributedString, search CFMutableAttributedString on stackoverflow and most answers will give you an idea of how to use this. For e.g. this question
Upvotes: 2
Reputation: 337
I think these are what you are looking for
void CGContextSelectFont (
CGContextRef c,
const char *name,
CGFloat size,
CGTextEncoding textEncoding
);
and
void CGContextSetFontSize (
CGContextRef c,
CGFloat size
);
Something like...
CGContextSelectFont(currentContext, "Helvetica", 20, kCGEncodingMacRoman);
or
CGContextSetFontSize ( currentContext, 20);
Hope that helps, here is more... Quartz 2D Programming Guide - Text
Upvotes: 2