the codemonky

ramblings, musing and code snippets from shawn veader
Mar 04
Permalink

iPhone Dev : Determining Text Height

OK, so you’re developing your application and programmatically laying out your UILabel. You want to know the height the label will ultimately be, so you can set the frame accordingly. You poke around and find the following method buried in NSString.


sizeWithFont:forWidth:lineBreakMode:

Thinking you’ve struck gold, you proceed. Once you run your app you realize this method will never give you a height for more than one line. This has to be a bug, you think to yourself.

http://openradar.appspot.com/radar?id=74427

Nope! Apple says it works as intended. This seems like a waste because you can get the same information out of UIFont. Sigh.

You end up having to go with NSString’s sizeWithFont:constrainedToSize:lineBreakMode: method and using a constraint with unlimited height. Here’s the full scoop.


// determine your width before and put it here
CGSize constrainedSize = CGSizeMake(width, MAXFLOAT);
UIFont *textFont = [UIFont systemFontOfSize:13.0f];
NSString *text = @"Some very long string goes here...";
CGSize textSize = [text sizeWithFont:textFont 
                   constrainedToSize:constrainedSize 
                       lineBreakMode:UILineBreakModeWordWrap];

Hopefully some day Apple will fix that first method to do the sane thing, but don’t hold your breath.

Feb 05
Permalink

Running the iPhone 3.2 Beta... First Gotchas

I installed the 3.2 beta the other day to play with the new iPad simulator. (Neat stuff… more on that later.) But I tried working on one of my many half-completed iPhone apps the other night and ran into a few interesting problems.

The first problem I encountered was that the credentials in my app were not saving to the user defaults. I scratched my head a few times with this but did get some good testing out of it.

The second problem was the one that eventually led me to hitting Google in search of answers. After getting past some bugs I uncovered by not having user credentials, my app tried to load a standard table view. This table view has a custom cell loaded from a NIB. When the app tried to load this NIB, I was greeted with this wonderful message in the console.

uncaught exception ‘NSInvalidUnarchiveOperationException’, reason: ‘*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (UITableViewCellContentView)’

A few Google searches later and I found this response over on StackOverflow. However, I found it hard to believe that all of a sudden I needed to declare a class that was supposed to be built into the framework. I then started poking at XCode and the simulator and noticed something strange….

I had opened my project from the console (yes, I’m *that* guy) with the “open” command as I always do. Unbeknownst to me this opened the newer XCode out of the alternate install. This turned out to be the root of all my problems. I opened /Developer/Applications/XCode.app and BOOM everything started working again.

So if you’re running dual development environments, watch out!

Jan 07
Permalink

Obj-C Oddities

I never seem to find enough time to be in Obj-C land long enough to develop muscle memory so there are things that consistently get me. NSDictionary creation is backwards:

NSDictionary *stuff = 
[NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", 
                                           @"value2", @"key2", nil];
In Ruby-land (where I’ve been for quite some time) it would be:
stuff = { :key1 => "value1", :key2 => "value2" }
Almost forgot! I also almost always forget the nil at the end of an NSDictionary or NSArray declaration. For instance:
NSArray *myList = [NSArray arrayWithObjects: @"foo", @"bar", @"baz", nil];
I continually forget that trailing nil, but it should be obvious since C is under the covers after all.

Jul 28
Permalink

Zombies are your friend!

No, they’re not here to eat your brain this time. They are here to save it.

So you’re happily coding along in Obj-C and developing the next big iPhone app. You build and run in the simulator only to see it crash and give EXC_BAD_ACCESS in the console. You then spend a few minutes to a few hours (depending on how persistent you are) debugging and trying to chase down the problem (which, by the way, is usually trying to send a message to a released object). Compound this by having a multithreaded application (you are using threads, right?) and you can start to feel the frustration building.

Now envision some zombies coming to help. Yes, zombies.

Well… not real zombies of course.


Objective-C was kind enough to include an awesome environment variable you can set that will leave around a dummy object for everything that is released so you can see where that EXC_BAD_ACCESS is coming from.

The environment variable is NSZombieEnabled. Edit the executable options in XCode and create a new environment variable named NSZombieEnabled and set the value to YES. It should look like this:

Now build and run and see what you get. Enjoy!

Jun 24
Permalink

Animated GIFs and UIImageViews

OK, so while building an iPhone app last night I ran into a little snag. We are using an animated GIF on the loading screen to allow the user to see something pretty while they wait.

Being naive, I assumed the UIImageView would display the animated GIF. Well… I was partially right. It does display the GIF but only the first frame.

Then the Google hunt began…

After some searching, I had found that you can set the animatedImages property of a UIImageView to a NSArray that contains each frame of the animated GIF and then call the startAnimating method to “play” it.

OK, great…. now how do I get the frames out? After more Googling I just tried Preview.app on a wild hair. I’m glad to report that it shows the animated GIF but in the drawer of the app, it also has each frame. Simply drag each of these out and boom! you’re set.

Now for the code sample.

UIImage *frame0 = [UIImage imageNamed:@"frame0.png"];
// ... more frames here ...
UIImage *frameN = [UIImage imageNamed:@"frameN.png"];

// animatedGif is a UIImageView initialized in your NIB or elsewhere.
animatedGif.animatedImages = [[NSArray alloc] initWithObjects:frame0
                                                              ....
                                                              frameN,
                                                              nil];
[animatedGif startAnimating];

There are a few other things you can play with like the animatedDuration and animatedRepeatCount properties. But you can figure those out.

And a link to the UIImageView documentation for completeness… UIImageView

Jun 16
Permalink

Must haves for Safari

So I’m back on Safari, for the moment, (no offense Firefox) and I just wanted to pass along two plugins that you must install.


Click-to-Flash

http://github.com/rentzsch/clicktoflash/tree/master

This replaces any flash on a website with a little logo that says “Flash”. If you actually want to see the flash you just click it and it loads. Works very well to keep the browser from chewing up all your computing resources running Flash. (Adobe: why is Flash so horrible?)


AdBlock

http://burgersoftware.com/en/safariadblock

Recent addition for me but blocks ad content for a more pleasant browsing experience.

Apr 23
Permalink

Starting down the path of objectifying C

OK, so I’ve had my head in and out of books lately trying to learn obj-C and Cocoa. So far it’s not too bad. Kinda fun to be back into a little more strict C-style world, while still maintaining some cool OO features.

My biggest gotchas/gripes I’ve found so far?

1) Having to add a nil to the end of a declared NSArray or NSDictionary (or their mutable variants).

[NSArray arrayWithObjects:@"1", @"2", @"3", nil];

That just seems like something that could be abstracted away for you…

2) Declaring a NSDictionary with keys and values is backward from what I would expect from Ruby land.

// what I expect
[NSDictionary dictionaryWithObjectsAndKeys:
              @"key1", @"value1", @"key2", @"value2",nil];
// what reality is
[NSDictionary dictionaryWithObjectsAndKeys:
              @"value1", @"key1", @"value2", @"key2",nil];

3) And as you can see from the examples above… the biggest gotcha of all is that to declare a string you have to prefix it with @.

(Hopefully I’ll start actually putting up stuff on this tumblelog again now…)

Jan 06
Permalink

Orkut.com.br needs to die!

Yes, I’m just one of the many who have fallen into the stupid Brazilian Orkut fake kid pages. There is a whole profile based off images from my Flickr stream.

http://www.orkut.com/Main#Album.aspx?uid=6447389076309092886&aid=1230630259
http://www.orkut.com/Main#Album.aspx?uid=499062161829329821&aid=1

I know I could toggle the “Friends & Family” setting on Flickr but I have too many people who look at my stream who are real friends and family that I don’t want to have to send a link or require them to sign up to see the pictures.

Of course Google, the owner of Orkut.com, is of absolutely no help. Reporting abuse on the site gives me this wonderful canned response:

Thank you for your reporting of abuse on Orkut on “2009-01-06”.

Based on our review and consideration of Orkut’s terms of service, we understand that this content does not currently break any policies on Orkut. If you believe that a mistake has been made, please re-submit your complaint with additional information to help our support team better understand your concerns with this content.
Thanks Google. Thanks!!!!?!?!

Glad they are so willing to help fight their site turning into a steaming pile of crap with pedophiles, etc.

Sigh, such is life on the interwebs.

Update: a friend of a friend works at Google and was able to get at least one of the accounts nuked this morning.
Nov 14
Permalink

Testing... Testing... This thing on?

Well it looks pike I forgot about my tumblog for some time now. Guess it is time to resurrect it.

Stay tunes for ramblings about languages I’m learning at the moment.

First up…. Erlang.

Jun 20
Permalink
Google does Atlanta

Google does Atlanta