Showing posts with label xcode. Show all posts
Showing posts with label xcode. Show all posts

Thursday, May 14, 2009

Camera image orientation

I was having a confusing problem where images taken from the iPhone camera were being displayed in the wrong orientation. I would take a photo in portrait and save the data to disk. Later when I retrieved the photo it would show up rotated 90 degrees.

The iPhone knows what orientation a photo is taken, and this orientation depends on how the camera is being held when you snap the photo. You can get the orientation value on a UIImage from the property imageOrientation.

When you save the photo, it is possible that this information could get lost, depending on the way you save it. If it does get lost, the UIImageView will assume the photo was taken as a left-side landscape photo, and display it with that orientation.

I ran into a problem when trying to save the camera image with UIImagePNGRepresentation(). For some reason this loses the orientation info. I switched to UIImageJPEGRepresentation() and all is well now.

So basically if you run into the problem, you have 2 choices: Be sure you use a format that preserves orientation data, or rotate the UIImage data directly before saving it to a file.

Sunday, March 29, 2009

iPhone 2.2.1 available fonts

Here is a list of the fonts available to the iPhone as of SDK 2.2.1:


Family: Courier
Font: Courier
Font: Courier-BoldOblique
Font: Courier-Oblique
Font: Courier-Bold
Family: AppleGothic
Font: AppleGothic
Family: Arial
Font: ArialMT
Font: Arial-BoldMT
Font: Arial-BoldItalicMT
Font: Arial-ItalicMT
Family: STHeiti TC
Font: STHeitiTC-Light
Font: STHeitiTC-Medium
Family: Hiragino Kaku Gothic ProN
Font: HiraKakuProN-W6
Font: HiraKakuProN-W3
Family: Courier New
Font: CourierNewPS-BoldMT
Font: CourierNewPS-ItalicMT
Font: CourierNewPS-BoldItalicMT
Font: CourierNewPSMT
Family: Zapfino
Font: Zapfino
Family: Arial Unicode MS
Font: ArialUnicodeMS
Family: STHeiti SC
Font: STHeitiSC-Medium
Font: STHeitiSC-Light
Family: American Typewriter
Font: AmericanTypewriter
Font: AmericanTypewriter-Bold
Family: Helvetica
Font: Helvetica-Oblique
Font: Helvetica-BoldOblique
Font: Helvetica
Font: Helvetica-Bold
Family: Marker Felt
Font: MarkerFelt-Thin
Family: Helvetica Neue
Font: HelveticaNeue
Font: HelveticaNeue-Bold
Family: DB LCD Temp
Font: DBLCDTempBlack
Family: Verdana
Font: Verdana-Bold
Font: Verdana-BoldItalic
Font: Verdana
Font: Verdana-Italic
Family: Times New Roman
Font: TimesNewRomanPSMT
Font: TimesNewRomanPS-BoldMT
Font: TimesNewRomanPS-BoldItalicMT
Font: TimesNewRomanPS-ItalicMT
Family: Georgia
Font: Georgia-Bold
Font: Georgia
Font: Georgia-BoldItalic
Font: Georgia-Italic
Family: STHeiti J
Font: STHeitiJ-Medium
Font: STHeitiJ-Light
Family: Arial Rounded MT Bold
Font: ArialRoundedMTBold
Family: Trebuchet MS
Font: TrebuchetMS-Italic
Font: TrebuchetMS
Font: Trebuchet-BoldItalic
Font: TrebuchetMS-Bold
Family: STHeiti K
Font: STHeitiK-Medium
Font: STHeitiK-Light

Wednesday, March 25, 2009

Perl Compatible Regular Expressions with Cocoa

If you want Perl Compatible Regular Expressions with Cocoa, Christopher Bess has created ObjPCRE, a library that makes PCRE easy in Cocoa.

However, there isn't much for documentation, so I thought I'd at least show how to get started. Implementation is pretty straight forward. First, you need to add the following files to your XCode project:

libpcre.a
pcre.h
objpcre.h
objpcre.m

You can find the libpcre.a file in the pcre static lib download, and the other three files are in the source file download. I created a new "PCRE" group folder in my XCode project and dropped them all in there.

Now, its just a matter of using it. So first, lets create a one-liner that search/replaces text in a string. We'll search for "string" and replace it with "foobar".

#import "objpcre.h"

NSString *myText = @"This is my string of text.";
NSLog(@"text before: %@", myText);
[[ObjPCRE regexWithPattern:@"string"] replaceAll:&myText replacement:@"foobar"];
NSLog(@"text after: %@", myText);


There you go, your first one-line to search/replace a string of text inline with perl regular expressions. Now this isn't very interesting, as no regular expressions were used. So now, let's try something useful. How about a regular expression that removes all HTML tags from the string. Lets try to think of a regex that will match every HTML tag:

<.*>

Ok that one is pretty basic. It says match <, followed by zero or more of ANY character, followed by >. This could cause a problem because it can match too much, such as multiple html tags along with any text between them. So we'll go with something a bit more restrictive:

<\w+[^>]*>

Now we will only match <, followed by one or more word characters (letter, number, underscore), followed by zero or more characters that are NOT >, followed by >.


We still have a problem though, this will not match closing HTML tags.

</?\w+[^>]*>

There, now we match tags with 0 or 1 "/" after the opening tag.

Notice that backslashes must be escaped inside @"double quotes", so we use two of them in the string.

#import "objpcre.h"

NSString *myText = @"<title>This is my <b>string</b> of <class name="foo">text</class>.</title>";
NSLog(@"text before: %@", myText);
[[ObjPCRE regexWithPattern:@"</?\\w+[^>]*>"] replaceAll:&myText replacement:@""];
NSLog(@"text after: %@", myText);


And now for something a bit trickier. Let's try extracting all words within [brackets] in the text. This is where ObjPCRE could use some more features! But for now, here is how we accomplish this task. First the regular expression that matches the tags:

\[\w+\]

The brackets have special meaning to PCRE, so we have to escape them. This matches [, followed by one or more word characters, followed by ]. But, lets say we want to capture just the text, without the brackets. We put parenthesis around each subpattern we want to capture. These will have no affect on the regex.

\[(\w+)\]

And now we put this into code. Remember to escape backslashes.

NSString *myText = @"This is [some] more [text] to parse.";

ObjPCRE *pcre = [ObjPCRE regexWithPattern:@"\\[(\\w+)\\]"];

int start = 0;
int len = 0;
int offset = 0;
int i = 0;
while([pcre regexMatches:myText options:0 startOffset:offset]) {
for(i=0; i<[pcre matchCount]; i++) {
NSLog(@"match %d: %@",i,[pcre match:myText atMatchIndex:i]);
}
[pcre match:&start length:&len atMatchIndex:0];
offset = start + len;
}


We call regexMatches for each [bracket] pattern it finds. For each of those we loop over the subpatterns and echo them. The first subpattern is the entire match, followed by each parenthesized subpattern (which we have only one.)

Alright, so that's a start! To continue, check all the functions available in objpcre.h, and also see the documentation on PCRE for all the good regex stuff.

Tuesday, January 27, 2009

Renaming an XCode Project from Command Line

UPDATE: As of Xcode 3.2 you can rename projects from the Projects->Rename dropdown.

Earlier I had posted how to manually rename an XCode project. I have now written a BASH shell script to handle everything automatically. You can find it here.

Wednesday, December 17, 2008

Renaming an XCode project

UPDATE: As of Xcode 3.2 you can rename projects from the Projects->Rename dropdown.

UPDATE: Be sure to see my post about the shell script I wrote to rename from the command line.


For those wanting to rename their XCode project, here how you do it manually:

1) copy your project to a new folder, rename the folder to your new project name, delete the build directory in the new folder.

2) drag the entire new project folder onto the TextMate text editor

3) In TextMate, remove (delete references) to anything non-text such as images and sounds. [UPDATE: TextMate will ignore the non-text files, so step 3 is unnecessary.]

4) Replace in Project (old project name) with (new project name), save all files. Close TextMate.

5) Now drag the (project name).xcodeproj file into Text Mate, repeat Replace in Project processm, save all files. Close Text Mate. If you have spaces in your project name, look for all instances of your project with spaces replaced with underscores too.

6) Rename all files containing (old project name) to (new project name). Be sure to check the Classes directory too. If your project name contains spaces, look for all instances of filenames with spaces replaced with underscores too.

That's it! Your new project should be good to go. I don't think cleaning of targets is necessary, since the build directory is removed (correct me if I'm wrong though!)