PDF creation and text formatting - objective-c

Question about text formatting when creating a PDF programmatically on the iPhone.
I have a formatted NSString with Tabs (\t) in it. On the debugging output it looks perfect and fine, but when this strings is used to generate a PDF, the Tabs are gone and replaced by a single space.. Any idea?
( Tried a font like Courier, no luck, also the page boundary is big enough, there is more then enough room to display the text)
This is the code I use to make the PDF:
- (void)drawStuffInContext:(CGContextRef)ctx {
UIFont *font = [UIFont fontWithName:#"Arial" size:10];
CGRect textRect = CGRectInset(kPDFPageBounds, 36, 36);
[textHolder drawInRect:textRect withFont:font];
}

You're not very clear in describing what exactly you're doing.
But the first thing I would try is this:
Replace your tabs with spaces yourself -- use as many spaces for each tab as make you happy.
Then convert this new string/text to PDF.

Related

Objective C - bold and change string text size for drawing text onto pdf

I've created a .xib file with a uiview solely for the pdf document that I have created. I've primarily followed this tutorial for code:
http://www.raywenderlich.com/6818/how-to-create-a-pdf-with-quartz-2d-in-ios-5-tutorial-part-2
From the above, I can't edit labels (plain or attributed) from the interface builder as even after changing size, it doesn't apply when the app is run. (I suspect the issue may lie within the methods drawText or drawLabels).
I then tried to create an attributed string, however the methods don't take it. I tried to convert the drawtext by copying and taking the argument of an nsmutableattributedstring however this caused problems with the CFStringRef.
I appreciate your time for any help.
Solved it by making a separate method as below (I used + since I have this inside an NSObject and is a class method rather than in a UIViewController):
+(void)addText:(NSString*)text withFrame:(CGRect)frame withFont:(UIFont*)font;
{
[text drawInRect:frame withFont:font];
}
Outside the method, declaring inputs and calling it:
UIFont *font = [UIFont fontWithName:#Helvetica-Bold" size:7];
[self addText: yourStringHere
withFrame:CGRectMake(yourXPosition, yourYPOsition, yourFrameWidth, yourFrameHeight)
withFont:font];
I wonder if it's much simplified like so because the tutorial I used is now outdated?
If anything I hope this helps others :)

How to call images to a different view in xcode using variables

I am currently working on a project in Xcode 4.4 with objective-c not in cocos2d where I have a view called GameView and a class called Characters. In the GameView have, well, the game, this is the scene in which you play the game. In the Characters, all the characters images and powers or anything unique to them are called. I want it so that I can choose a character in CharacterSelect page and then the character that I selected is the character that is in the GameView and that I am controlling. Right now I am just working with the images. In the GameView I am basically trying to set up a way to call an image of the corresponding character from the Characters class. If I could I would have an animation of the character but I'm just going to go step by step. All I want is the corresponding image for the character while he's facing right
Here is my code in my GameView.m for the function to basically create the image, and I know I need to call the image file from a variable in Characters. bluebox is like defined in the .h as a UIImageView and really it just contains all the things the character can do... such as the left and right buttons allows it to move and so on...
-(void)createCharacter
{
bluebox = [[UIImageView alloc] initWithFrame:
CGRectMake(70, 100, 150, 130)];
bluebox = [[UIImageView alloc] init];
}
In my Characters.m I made an NSString in my .h of Characters in it is called lookingRight1... And then for one of my characters I have this
-(void)megaMan
{
lookingRight1 = #"MegaManRight1.png";
}
Now I suppose I'd need to call looking right. I'm actually confusing my self trying to write this, so please help, if you don't get confused also. I don't have really much code for my CharacterSelect page but what I do have just links a particular button labeled with a character name to the GameView.
If you could please help I'd be extremely thankful. I think that it would help if you could show a mock second character also so that I can see what to do to get the character I want...
Sorry If this is confusing to you.

DBLCDTempBlack only for some letters in UILabel

I am uising this code for marquee effect. i want digital clock like font so i gave font as
UIFont *lcdFont = [UIFont fontWithName:#"DBLCDTempBlack" size:60.0];
self.font = lcdFont ;
But i am getting this font only for some random characters.I am using only uppercase letters. I want i for all the characters in UILabel.
Why is it happening so.
It's nothing wrong with your code -- that font only has certain characters in it. (Why? Who knows.) For those it's missing, the OS automatically falls back to another font.
If you want to draw LCD-looking text using a broader character set, you'll need to find such a font elsewhere and bundle it in your app.

How do I calculate the exact height of text using UIKit?

I'm using -[NSString sizeWithFont] to get the text height. The character 't' is clearly taller than 'm', but -sizeWithFont returns the same height for both these strings. Here's the code:
UIFont* myFont = [UIFont fontWithName:#"Helvetica" size:1000.0];
NSString* myStr = #"m";
CGSize mySize = [myStr sizeWithFont:myFont];
With 'm' as shown, it returns {834, 1151}. With myStr = #"t" instead, it's {278, 1151}. The smaller width shows up as expected, but not the height.
Does some other function wrap the text tightly? I'm ideally looking for something equivalent to Android's Paint.getTextBounds().
The information you get back from this method is basically the line height for the font (i.e., it's the ascent plus the descent for the font you've chosen). It's not based on individual characters, it's based on specific font metrics. You can get most of the information about a font's metrics from the UIFont class (e.g., the method -ascender gives you the height of the ascender for the font). Mostly, you will be dealing with the total amount of vertical space needed to draw the glyphs with the heights ascenders and the lowest descenders for that font. There is no way to get information about individual glyphs from UIFont. If you need this information, you'll have to look at the CoreText framework, which gives you a lot more flexibility in how you draw and arrange glyphs but is far, far more complicated to use.
For more information on dealing with text in your app, please se the Drawing and Managing Text section of the Text, Web, and Editing Programming Guide. It is also a good launching point for most of the frameworks and classes you'll need to deal with whether you go the UIKit or the CoreText route.
Hmmm... I assume you're only going to be working with individual characters then. sizeWithFont, as noted above, returns the height of the largest character of that font as the text field is going to be that height no matter what you do. You would be able to get the LARGEST height values (UIFont's CGFloat capHeight) however it looks like you're going to be working with all kinds of text
I would take a look at the CoreText framework. Start here and read away. Inevitably you're going to end up with something along the lines of this:
CGFloat GetLineHeightForFont(CTFontRef iFont)
{
CGFloat lineHeight = 0.0;
check(iFont != NULL);
lineHeight += CTFontGetLeading(iFont);
return lineHeight;
}

Unicode Character in Core Graphics

I have an iPad app where I am storing some unicode (non-ASCII) data in a SQLite DB. Later, I retrieve that data and need to write it to a pdf. All ASCII data does fine, but the unicode data is presenting as "encoded characters"
The process goes like this:
Retrieve the data and write it to the console. This goes fine. Here is the output to the console: LOOKUP:↑ to enable all activities:FOR:COORDINATIONGOAL:
(The little up arrow is my unicode character)
At this point, the data is stored in an NSString. So next, I convert it to a char so I can use it in core graphics:
NSString *t = [data objectForKey:pi.persistencekey];
char *text = " ";
if ([t length] > 0) {
text = [t UTF8String];
}
CGContextShowTextAtPoint (pdfContext, pi.x, pageRect.size.height - pi.y, text, strlen(text));
The PDF generates fine, but the text generated shows strange characters where the up arrow is supposed to be.
I have tried other decoding methods and none work.
Thanks in advance for any help.
CGContextShowTextAtPoint has some known weaknesses with drawing unicode. Basically it doesn't.
You have two options:
NSString's drawAtPoint and it's friends. They can be found by searching doc for UIStringDrawing.
or if you need to go low-level, Core Text.
Most of the time (99%) 1. is good enough.