Friday, 26 August 2011

Improved ListAllFonts

I was playing around with the "ListAllFonts" project I had before, but noticed a problem: although it's quite handy to have al the font names available, it's not much use unless I can visually see what each font looks like. Changing the font of a label one font name at a time is just too dumb...

Also added 2 new things just learned: block-code and UIScrollView.

A simple block-code to show font and automatically increase the "y" value is as below. It accepts 3 parameters: the view (UIScrollView), the string to be displayed and the font name.


void (^showFonts)(UIView *,NSString *,NSString *) = ^(UIView *uView, NSString *msg,NSString *fName) {
    UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(xx, yy, uView.frame.size.width, fontSize*2)];
    [tmpLabel setText:msg];
    [tmpLabel setFont:[UIFont fontWithName:fName size:fontSize]]; 
    [tmpLabel setTextColor:[UIColor blackColor]];
    [tmpLabel setBackgroundColor:[UIColor clearColor]];
    [tmpLabel setShadowColor:[UIColor yellowColor]];
    [tmpLabel setOpaque:NO];
    [tmpLabel setAlpha:1.0];
    [uView addSubview:tmpLabel];
    [tmpLabel release];
    yy+=fontSize*1.5;
    
};


The UIScrollView is also quite simple, I was stuck with the "content size" for a while - I set it to be the same as current screen size in the beginning and was scratching my head wondering how come it wouldn't scroll??  Well, of course it won't, unless I set it to a much bigger size....


    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];   
    [scrollView setScrollEnabled:YES];
    [scrollView setContentSize:CGSizeMake(screenWidth,(1000-screenHeight)*25)];
    [self.view addSubview:scrollView];
    [scrollView release];


The result is quite nice, as shown below.

And yes, with the limited display area, how could I forget about the landscape mode? So I also changed it a bit so that it automatically redisplay the content if the device orientation changed:

Note: hasn't got time to run through instruments to check for memory leak or other problems - please let me know if you noticed any.

Enjoy!

Source to V2 of ListAllFonts

[Update 05/04/2012] Found this web site which listed out all iOS fonts for iOS5, which you might also find it handy.

[Update 03/05/2012]
I run it with iPhone 4.3 simulator, it listed 42 different fonts.
Then run it with iPhone 5.0/5.1 simulator, both listed 58 different fonts.

No comments:

Post a Comment