Thundercats 2011

This has very little to do with iOS. But i love it anyways.. so here we go!. When i was … less than 4 years old, i was a big fan of the Thundercats. Back then, i had a copy of the Sword of Omens. Never managed to get a glimpse of the real one… wherever it is!.

Last year, Cartoon Network fellows published a reboot of the Thundercats. It’s called ‘Thundercats 2011’. So far, i just love it. The story is a bit different… either way, this re-imagined version is pretty cool.

No. I’m not an anime dude. Furthermore, i don’t usually watch cartoons. But this one is different, because it remind me of the oooooold school toons i used to watch.

Fixing Bootstrap Errors

I’ve been having this error… A LOT…:

“Couldn’t register com.yourcompany.yourapp with the bootstrap server. Error: unknown error code. This generally means that another instance of this process was already running or is hung in the debugger.”

The bad side of this is that.. ps aux will not show anything useful. Thankfully, Mike Ash has solved this… i’m pasting below his script:

[cc lang=”bash”]
launchctl list|grep UIKitApplication|awk ‘{print $3}’|xargs launchctl remove
[/cc]

Source: Mike Ash Blog

Removing subviews that match any given criteria!

So… suppose that you have a container view. And for some reason, you need to remove the subviews that match any given criteria.

The straightforward solution would be to write a foreach loop, by hand, and remove the target subviews with a method call. Guess what!. There is a kung fu solution to this..!!. Check this out:

[cc lang=”objc”]
NSPredicate* predicate = [NSPredicate predicateWithFormat:@”self isKindOfClass: %@”, [SomeView class]];
NSArray* viewsToRemove = [[self subviews] filteredArrayUsingPredicate:predicate];
[viewsToRemove makeObjectsPerformSelector:@selector(removeFromSuperview)];
[/cc]

Less code is better. Always.

Detecting taps in any UIView subclass

This one is a quick and easy trick. How do you detect taps in any UIView?.
Simple. You need to use the UITapGestureRecognizer class… this way:

[cc lang=”objc”]
// Alloc the gesture recognizer
UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[anyRandomView addGestureRecognizer:tapGestureRecognizer];
[anyRandomView setUserInteractionEnabled:YES];

[tapGestureRecognizer release];
tapGestureRecognizer = nil;
[/cc]

UILabel with Stroke!

Time to invoke some… dark magic. For some reason, UILabel doesn’t support stroke. So, if you’re… by chance… working on a videogame, or a simple iOS app, and you actually need to render an UILabel with a stroke, in a given color, you have come to the right place.

Let’s begin with the header file:

[cc lang=”objc”]
@interface LAStrokeLabel : UILabel
{
NSUInteger _strokeWidth;
   UIColor* _strokeColor;
}

@property (nonatomic, assign) NSUInteger strokeWidth;
@property (nonatomic, retain) UIColor* strokeColor;

@end
[/cc]

So far so good… right?. Nothing weird. Just a simple UILabel subclass, with two extra properties.

Now, let’s get to business. We’re gonna need to link ‘CoreGraphics’ framework. Otherwise this won’t work. The .m file should look like this:

[cc lang=”objc”]
static NSUInteger kDefaultStrokeWidth = 1;

@implementation LAStrokeLabel

@synthesize strokeWidth = _strokeWidth;
@synthesize strokeColor = _strokeColor;

-(void)dealloc
{
[_strokeColor release];
_strokeColor = nil;

[super dealloc];
}

-(id)init
{
if((self = [super init]))
{
_strokeWidth = kDefaultStrokeWidth;
_strokeColor = [[UIColor blackColor] retain];
}

return self;
}

-(id)initWithFrame:(CGRect)frame
{
if((self = [super initWithFrame:frame]))
{
_strokeWidth = kDefaultStrokeWidth;
_strokeColor = [[UIColor blackColor] retain];
}

return self;
}

-(void)awakeFromNib
{
_strokeWidth = kDefaultStrokeWidth;
_strokeColor = [[UIColor blackColor] retain];

[super awakeFromNib];
}

-(void)drawTextInRect:(CGRect)rect
{
CGSize shadowOffset = self.shadowOffset;
UIColor* textColor = self.textColor;
BOOL highlighted = self.highlighted;

CGContextRef c = UIGraphicsGetCurrentContext();

// Draw the stroke
if( _strokeWidth > 0 )
{
CGContextSetLineWidth(c, _strokeWidth);
CGContextSetTextDrawingMode(c, kCGTextStroke);

self.textColor = _strokeColor;
self.shadowColor = _strokeColor;
self.shadowOffset = CGSizeMake(0, 0);
self.highlighted = NO;

[super drawTextInRect:rect];
}

// Revert to the original UILabel Params
self.highlighted = highlighted;
self.textColor = textColor;

// If we need to draw with stroke, we’re gonna have to rely on the shadow
if(_strokeWidth > 0)
{
self.shadowOffset = CGSizeMake(0, 1); // Yes. It’s inverted.
}

// Now we can draw the actual text
CGContextSetTextDrawingMode(c, kCGTextFill);
[super drawTextInRect:rect];

// Revert to the original Shadow Offset
self.shadowOffset = shadowOffset;
}

@end
[/cc]

If you figured out… you just got an extra point. Yes. For some reason, CoreGraphics’s stroke wasn’t drawing anything ‘below the bottom line’. That’s the reason why i’ve implemented a workaround: the ‘bottom’ of the stroke is actually a shadow.

A bit hacky, but i promess, it will work great.