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, March 24, 2009

Reset a UIScrollView

This has to be the most puzzling thing I've come across in iphone app building thus far. I wanted to "reset" a UIScrollView after pinch zooming and scrolling. It turns out, there is no way to reset the zoom factor on the contentView in a UIScrollView. At least not in the current SDK. You must completely replace the subview of the scroll view to work around it.

I wanted this transition to happen smoothly, so I used some core animation. Here is some working code to get the scroll view to "reset". In my app, I implemented this after a double tap, and after an orientation change.

self.scrollView is the UIScrollView, and self.imageView is the UIImageView subview.


- (void) resetImageZoom {

// animate the transition
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(resetAnimFinish:finished:context:)];
[UIView setAnimationDuration:0.4];

// reset the scrollview and the current image view
self.scrollView.transform = CGAffineTransformIdentity;
self.scrollView.contentOffset = CGPointZero;
self.imageView.frame = self.scrollView.frame;
self.imageView.center = self.scrollView.center;
self.scrollView.contentSize = self.imageView.frame.size;

[UIView commitAnimations];

}

-(void)resetAnimFinish:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

// make a copy of the image view
UIImageView *copy = [[UIImageView alloc] initWithImage:self.imageView.image];
copy.autoresizingMask = self.imageView.autoresizingMask;
copy.contentMode = self.imageView.contentMode;
copy.frame = self.imageView.frame;
copy.center = self.imageView.center;

// replace the current image view with our copy
[self.imageView removeFromSuperview];
self.imageView = copy;
[self.scrollView addSubview:copy];
[copy release];
}

If you wanted, you could extend UIImageView an implement the NSCopying protocol, then just use the copy convenience method. I chose to copy the object a bit more manually here.

Sunday, March 22, 2009

stripping HTML with objective-c/cocoa

I was looking for a simple way to strip HTML from an NSString. Since NSString has no native regular expression support, I had to resort to other means. I found many posts requiring regex support libs and/or libxml2 support. Bleh. Then I found this simple solution using NSScanner. It worked well for me.

It's not super smart though. I'm guessing it will bork on any stray < or > tags in the text that are not part of HTML markup. Make sure they are escaped.

http://www.rudis.net/content/2009/01/21/flatten-html-content-ie-strip-tags-cocoaobjective-c

I made my own small addition, optionally trimming whitespace too.


- (NSString *)flattenHTML:(NSString *)html trimWhiteSpace:(BOOL)trim {

NSScanner *theScanner;
NSString *text = nil;

theScanner = [NSScanner scannerWithString:html];

while ([theScanner isAtEnd] == NO) {

// find start of tag
[theScanner scanUpToString:@"<" intoString:NULL] ;
// find end of tag
[theScanner scanUpToString:@">" intoString:&text] ;

// replace the found tag with a space
//(you can filter multi-spaces out later if you wish)
html = [html stringByReplacingOccurrencesOfString:
[ NSString stringWithFormat:@"%@>", text]
withString:@" "];

} // while //

// trim off whitespace
return trim ? [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] : html;

}