Writing text on pdf ios - ios

Hello I have followed this guide to write text on pdf file. This guide maybe old but follows the same approach as in apple docs
What I have done so far:
PdfCreator.m
const int A4_WIDTH = 612;
const int A4_HEIGHT = 792;
#interface PdfCreator()
#property (nonatomic, assign) double currentHeight;
#end
#implementation PdfCreator
- (void) createPdfWithName:(NSString*)name{
// Create the PDF context using the default page size of 612 x 792.
UIGraphicsBeginPDFContextToFile(name, CGRectZero, nil);
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, A4_WIDTH, A4_HEIGHT), nil);
self.currentHeight = 0;
}
- (void) printTrip:(Trip*) trip{
// 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);
NSString* textToDraw = #"Hello World";
CFStringRef stringRef = (__bridge CFStringRef)textToDraw;
// Prepare the text using a Core Text Framesetter
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
//http://stackoverflow.com/questions/6988498
CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(
framesetter, /* Framesetter */
CFRangeMake(0, textToDraw.length), /* String range (entire string) */
NULL, /* Frame attributes */
CGSizeMake(A4_WIDTH, CGFLOAT_MAX), /* Constraints (CGFLOAT_MAX indicates unconstrained) */
NULL /* Gives the range of string that fits into the constraints, doesn't matter in your situation */
);
CGRect frameRect = CGRectMake(0, 0, suggestedSize.width, suggestedSize.height);
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);
// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, 100);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
CFRelease(frameRef);
CFRelease(stringRef);
CFRelease(framesetter);
}
- (void) endFile{
UIGraphicsEndPDFContext();
}
#end
Now I use it like this in another model file:
PdfCreator *pdf = [[PdfCreator alloc] init];
[pdf createPdfWithName:documentDirectoryFilename];
for (Trip *t in self.trips) {
[pdf printTrip:t];
}
[pdf endFile];
NSURL *URL = [NSURL fileURLWithPath:documentDirectoryFilename];
if (URL) {
// Initialize Document Interaction Controller
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
// Configure Document Interaction Controller
[self.documentInteractionController setDelegate:self];
// Preview PDF
[self.documentInteractionController presentPreviewAnimated:YES];
}
The problem is that it prints this:
I noticed that if I call only once the printTrip: method only one HelloWorld label is printed and in the correct position. Successive calls print the mirrored text on top. It is strange because this line
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
should reset the scaling factors. Any help would be appreciated.

Check out the documentation from Apple on CGContextSaveGState and CGContextRestoreGState here:
https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGContext/#//apple_ref/c/func/CGContextSaveGState
These two functions are commonly used in PDF files to bracket modifications to the current graphic state (which includes everything from color settings to clipping and the CTM or current transformation matrix).
Using your code:
CGContextSaveGState(currentContext); /// A
CGContextTranslateCTM(currentContext, 0, 100);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
CGContextRestoreGState(currentContext); /// B
At point B you're now back to exactly where you were at point A.
You can nest these, it's implemented as a stack. You have to be careful to keep them balanced though. And from the point of view of someone who's written PDF parser software, you also want to keep the number of save / restore pairs to what you actually need. Don't use them unnecessarily :)

The answer is found on part 2 of the tutorial. The context should be reset after the drawing of the text.
CGContextTranslateCTM(currentContext, 0, 100);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
/*NEW!*/
CGContextScaleCTM(currentContext, 1.0, -1.0);

Related

Vertical Text Alignment in Core Text

EDIT: Here's a Github Gist of the below code if that is more readable.
I'm writing a method to draw text within a rectangle in a PDF using Core Text. What I've written accomplishes everything I need, except vertical alignment (top, center, bottom). The standard way that the text is currently drawn is the top, and I specifically need the bottom alignment.
- (void)drawText:(NSString *)textToDraw inFrame:(CGRect)frameRect withFont:(UIFont *)originalFont textColor:(UIColor *)textColor alignment:(PDFTextAlignment)alignment verticalAlignment:(PDFTextVerticalAlignment)verticalAlignment {
if (!textToDraw) {
// If nil, give it an empty value to draw
textToDraw = #"";
}
// Prepare font
CTFontRef font = [self ctFontRefFromUIFont:originalFont];
CGColorRef color = textColor.CGColor;
// Paragraph
CTTextAlignment ctAlignment;
switch (alignment) {
case PDFTextAlignmentLeft:
ctAlignment = kCTTextAlignmentLeft;
break;
case PDFTextAlignmentCenter:
ctAlignment = kCTTextAlignmentCenter;
break;
case PDFTextAlignmentRight:
ctAlignment = kCTTextAlignmentRight;
break;
case PDFTextAlignmentJustified:
ctAlignment = kCTTextAlignmentJustified;
break;
default:
ctAlignment = kCTTextAlignmentLeft;
break;
}
CTParagraphStyleSetting settings[] = {
{kCTParagraphStyleSpecifierAlignment, sizeof(ctAlignment), &ctAlignment},
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0]));
// Create an attributed string
CFStringRef keys[] = { kCTFontAttributeName, kCTForegroundColorAttributeName, kCTParagraphStyleAttributeName };
CFTypeRef values[] = { font, color, paragraphStyle };
CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values,
sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFStringRef stringRef = (__bridge CFStringRef)textToDraw;
// Prepare the text using a Core Text Framesetter.
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, attr);
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.
// Modify this to take into consideration the origin.
CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
// Add these two lines to reverse the earlier transformation.
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);
CFRelease(frameRef);
CFRelease(stringRef);
CFRelease(framesetter);
CFRelease(font);
CFRelease(paragraphStyle);
}
Here is an example of how this will currently draw text in a rectangle (note the rectangle drawn with the lines is the same frameRect I'm passing in)...
.
I want this text to appear as bottom aligned in this rectangle, like this...
.
I've gone through the solutions on this SO post, but have not had any luck getting the text to align correctly.
I'm passing in an enumerated value for the vertical alignment, so ideally I will determine the vertical alignment with the following conditional.
if (verticalAlignment == PDFTextVerticalAlignmentTop) {
// Top align
} else if (verticalAlignment == PDFTextVerticalAlignmentCenter) {
// Center (vertical) align
} else if (verticalAlignment == PDFTextVerticalAlignmentBottom) {
// Bottom align
} else {
// Default: Bottom alignment
}
Thanks!
As I am not quite sure how you approached the solutions from the linked post, I will suggest a solution based on one of those answers. This may be exactly what you tried, but it may also be the appropriate solution.
I assume a method exists for obtaining the size of CTFrameRef, leaving that implementation to you, as long as the appropriate CGSize is returned. Apart from the methods shown in the linked SO post, it might also be worth looking at CTFramesetterSuggestFrameSizeWithConstraints. Assuming this function is available, it should be enough to adjust this part of the drawing function
...
CGMutablePathRef framePath = CGPathCreateMutable();
// Get the graphics context.
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGRect textRect = frameRect; // Will also give you the default behavior for top align
CGSize textSize = [self measureFrame:frameref forContext:currentContext];
if (verticalAlignment == PDFTextVerticalAlignmentBottom) {
// Bottom align
textRect.size.height = textSize.height;
textRect.origin.y = frameRect.origin.y + frameRect.size.height - textSize.height;
} else if (verticalAlignment == PDFTextVerticalAlignmentCenter) {
// Center (vertical) align
textRect.size.height = textSize.height;
textRect.origin.y = frameRect.origin.y + (frameRect.size.height - textSize.height) / 2;
}
CGPathAddRect(framePath, NULL, textRect);
...

iOS CoreText write to PDF and word wrapping

I'm using a slight modification of code from this excellent article from rawenderlich.com to write some text to a PDF:
-(void)drawText:(NSString*)text inFrame:(CGRect)frameRect withAttributes:(NSDictionary*)attributes
{
CFStringRef stringRef = (__bridge CFStringRef)text;
// Prepare the text using a Core Text Framesetter
CFDictionaryRef attributeRef = (__bridge CFDictionaryRef)attributes;
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, attributeRef);
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.
CGFloat offset = (frameRect.origin.y*2)+frameRect.size.height;
CGContextTranslateCTM(currentContext, 0, offset);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, -offset);
CFRelease(frameRef);
CFRelease(framesetter);
CFRelease(currentText);
}
This works well, but I cannot get the text to wrap when it is greater than the width of the frame. I simply get the ellipsis (...) at the end of my 'one line' of text.
This wraps when rendering to the screen so am wondering if I'm missing something for writing to pdf and flagging it to wrap. Can anyone offer suggestions?
Well after some research it seems that the LineBreak mode was set to TruncateTail. As the article referenced here uses a .xib file to specify the text etc., I was setting the attributed label text to Word Wrap but only from within the attributed text inspector for that label. From what I can see, this is ignored and I had to set the Line Breaks setting to Word Wrap to get the attribute change.

Creating multi page PDF from text content

I have a long text content and wanted to convert it in to a multipage PDF file.
I have followed the instructions provided in this tutorial http://www.raywenderlich.com/6581/how-to-create-a-pdf-with-quartz-2d-in-ios-5-tutorial-part-1.
But I am struggling to make the pdf multi page.
The code I'm using can create single page PDF.
+(void)drawText
{
NSString* textToDraw = #"The sample text";
CFStringRef stringRef = (__bridge CFStringRef)textToDraw;
// Prepare the text using a Core Text Framesetter
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
CGRect frameRect = CGRectMake(50, 50, 512, 1000);
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, 100);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
CFRelease(frameRef);
CFRelease(stringRef);
CFRelease(framesetter);
}
+(void)drawPDF:(NSString*)fileName
{
// Create the PDF context using the default page size of 612 x 792.
UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
[self drawText];
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}
you can draw pdf using following code:
// Use Core Text to draw the text in a frame on the page.
- (CFRange)renderPage:(NSInteger)pageNum withTextRange:(CFRange)currentRange
andFramesetter:(CTFramesetterRef)framesetter
{
// 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);
// Create a path object to enclose the text. Use 72 point
// margins all around the text.
CGRect frameRect = CGRectMake(72, 72, 468, 648);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
// Get the frame that will do the rendering.
// The currentRange variable specifies only the starting point. The framesetter
// lays out as much text as will fit into the frame.
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, kDefaultPageHeight);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
// Update the current range based on what was drawn.
currentRange = CTFrameGetVisibleStringRange(frameRef);
currentRange.location += currentRange.length;
currentRange.length = 0;
CFRelease(frameRef);
return currentRange;
}
- (void)drawPageNumber:(NSInteger)pageNum
{
NSString* pageString = [NSString stringWithFormat:#"Page %ld", (long)pageNum];
UIFont* theFont = [UIFont systemFontOfSize:12];
// CGSize maxSize = CGSizeMake(kDefaultPageWidth, 72);
CGSize pageStringSize = [pageString sizeWithAttributes:
#{NSFontAttributeName:
theFont}];
// CGSize pageStringSize = [pageString sizeWithFont:theFont
// constrainedToSize:maxSize
// lineBreakMode:NSLineBreakByClipping];
CGRect stringRect = CGRectMake(((kDefaultPageWidth - pageStringSize.width) / 2.0),
720.0 + ((72.0 - pageStringSize.height) / 2.0) ,
pageStringSize.width,
pageStringSize.height);
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attributes = #{NSFontAttributeName: [UIFont systemFontOfSize:15], NSParagraphStyleAttributeName: paragraphStyle};
[pageString drawInRect:stringRect withAttributes:attributes];
// [pageString drawInRect:stringRect withFont:theFont];
}
For save pdf:
- (IBAction)savePDFFile:(id)sender
{
NSString* path = [[NSBundle mainBundle] pathForResource:#"sampleData" ofType:#"plist"];
// get a temprorary filename for this PDF
path = NSTemporaryDirectory();
self.pdfFilePath = [path stringByAppendingPathComponent:
[NSString stringWithFormat:#"%d.pdf",
[[NSDate date]
timeIntervalSince1970] ]];
// Prepare the text using a Core Text Framesetter
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL,
(CFStringRef)textView.text, NULL);
if (currentText) {
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
if (framesetter) {
NSString* pdfFileName = self.pdfFilePath; //[NSString stringWithString:#"test.pdf"];
// Create the PDF context using the default page: currently constants at the size
// of 612 x 792.
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
CFRange currentRange = CFRangeMake(0, 0);
NSInteger currentPage = 0;
BOOL done = NO;
do {
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth,
kDefaultPageHeight), nil);
// Draw a page number at the bottom of each page
currentPage++;
[self drawPageNumber:currentPage];
// Render the current page and update the current range to
// point to the beginning of the next page.
currentRange = [self renderPage:currentPage withTextRange:
currentRange andFramesetter:framesetter];
// If we're at the end of the text, exit the loop.
if (currentRange.location == CFAttributedStringGetLength
((CFAttributedStringRef)currentText))
done = YES;
} while (!done);
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
// Release the framewetter.
CFRelease(framesetter);
} else {
NSLog(#"Could not create the framesetter needed to lay out the atrributed string.");
}
// Release the attributed string.
CFRelease(currentText);
} else {
NSLog(#"Could not create the attributed string for the framesetter");
}
// Ask the user if they'd like to see the file or email it.
UIActionSheet* actionSheet = [[[UIActionSheet alloc] initWithTitle:#"Would you like to preview or email this PDF?"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Preview", #"Email", nil] autorelease];
[actionSheet showInView:self.view];
}
here it is full demo please check: http://ge.tt/1eW8iJt1/v/0

UIGraphicsEndPDFContext(); fails with Japanese NSAttributedText

Has anyone encountered Xcode failing at UIGraphicsEndPDFContext() after drawing to a context with Japanese text from a NSAttributedString? Luckily the first time I tested my code in Japanese, my iPhone was off-line and it worked. However, much later when I ran the same code in the simulator, it failed and I spent a lot time time trying to debug it. It will also fail when trying to run it from Xcode on an iPhone or iPad when hardwired to the Mac. Seems like trying to run from Xcode in Japanese in this way is the problem. The Mac was set to English.
Following is the method I used for the drawing. It is closely based on the example published by Erica Sadun in her book, "The iOS 5 Developer's Cookbook" which was a good read early on for me. It is placed after a UIGraphicsBeginPDFContextToData() and UIGraphicsBeginPDFPageWithInfo() sequence and before a UIGraphicsEndPDFContext() call which all works fine in English. I don't necessarily need to fix this. I just wanted to alert others so that they might be able to avoid the two days I spent trying to figure this issue out.
- (void)drawText:(NSAttributedString *)textToDraw inFrame:(CGRect)frameRect bkgColor:(UIColor *)backgroundColor {
CFAttributedStringRef currentText = (__bridge CFAttributedStringRef)(textToDraw);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
// Draw background when a color is set
if (backgroundColor) {
[backgroundColor set];
CGRect backgroundFrame = CGRectMake(frameRect.origin.x, frameRect.origin.y -backgroundOffset, frameRect.size.width, frameRect.size.height -3);
//CGRect backgroundFrame = CGRectMake(frameRect.origin.x, frameRect.origin.y -2, frameRect.size.width, frameRect.size.height -3);
CGContextFillRect( UIGraphicsGetCurrentContext(), backgroundFrame);
[[UIColor whiteColor] set];
}
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 so no old scaling factors are left in place.
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
int adjust = frameRect.origin.y * 2 + frameRect.size.height;
// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, adjust);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, (-1)*adjust);
CFRelease(frameRef);
CFRelease(framesetter);
backgroundColor = nil;
}

iOS Dynamic PDF generation, with desired Text font type and font color

I am creating dynamic PDF from my application. In some cases i want my text to be write in PDF with desired color. how can i get that?
I am using CoreText.
Here is my code to draw text in my PDF,
+(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 will be appreciated.,
Thanks in advance.
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);
See my question, you will get an idea.

Resources