Development repo for Gentoo ebuilds!

That is all.

More fun with NSStrings in Objective C

Sometimes, it’s the simple things which can be the most handy. Here’s a quick category on NSString to allow all characters within a set (NSCharacterSet *illegalCharacterSet or NSCharacterSet *symbolCharacterSet, say) to be easily and efficiently removed. This fills a gap in between the stringByTrimmingCharactersInSet: and stringByReplacingCharactersInRange: withString: & stringByReplacingOccurrencesOfString: withString: methods, which only act upon the ends of the receiver or require a continuous range or fixed string respectively.


NSString truncate function for Objective C/iPhone SDK

So far as I can tell, the iPhone SDK exposes no method to truncate an NSString to a given width (in pixels). This function obviously exists since it is used when drawing UILabels, and you can even draw truncated text with the method:
- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont*)font lineBreakMode:(UILineBreakMode)lineBreakMode
… but there’s no way to read-back the rendered text.

A quick search of the ‘net revealed some methods which would truncate a string based on the number of characters, but nothing to perform the operation based on the rendered width in pixels.