Tag Archive for 'iphone'

Sizing labels with -[NSString sizeWithFont:…] – WRONG


CGSize maxSize = CGSizeMake(200.0f, CGFLOAT_MAX);
CGSize requiredSize = [label sizeThatFits:maxSize];
label.frame = CGRectMake(10, 10, size.width, size.height);

More details

iPhone – transparent imageview tint

Drop-in solution for a tintable/blendable “imageview”:

The .h file:

#import <UIKit/UIKit.h>

@interface UIBlendableImageView : UIView

- (id)initWithImage:(UIImage*)image andTintColor:(UIColor*)tintColor;


The .m file:

#import "UIBlendableImageView.h"
#import <QuartzCore/QuartzCore.h>

@implementation UIBlendableImageView

- (id)initWithImage:(UIImage*)image andTintColor:(UIColor*)tintColor
    self = [super initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    if (self) {
        UIImageView *maskImageView = [[[UIImageView alloc] initWithImage:image] autorelease];
        [maskImageView setFrame:[self bounds]];
        [[self layer] setMask:[maskImageView layer]];
        [self setBackgroundColor:tintColor];
    return self;


Remove all accents in NSString

// convert to a data object, using a lossy conversion to ASCII
NSData *asciiEncoded = [yourOriginalString dataUsingEncoding:NSASCIIStringEncoding

// take the data object and recreate a string using the lossy conversion
NSString *other = [[NSString alloc] initWithData:asciiEncoded
// relinquish ownership
[other autorelease];

Via Stackoverflow.com

Preventing backup to iCloud

5.1 and up

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
    NSError *error = nil;
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    return success;


#import <sys/xattr.h>
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
    const char* filePath = [[URL path] fileSystemRepresentation];
    const char* attrName = "com.apple.MobileBackup";
    u_int8_t attrValue = 1;
    int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
    return result == 0;


Reachability with ARC and GCD

This is a drop-in replacement for Apples Reachability class. It is ARC compatible, uses the new GCD methods to notify of network interface changes.

In addition to the standard NSNotification it supports the use of Blocks for when the network becomes reachable and unreachable.

Finally you can specify wether or not a WWAN connection is considered “reachable”.

Go to GitHub page

How to dispatch on main queue synchronously without a deadlock?

void runOnMainQueueWithoutDeadlocking(void (^block)(void))
    if ([NSThread isMainThread])
        dispatch_sync(dispatch_get_main_queue(), block);


Parsing date fields with NSDateFormatter ignoring locale

In many cases if the iOS device international settings is not set to US, you may have errors parsing text-based dates from e.g. RSS feeds.
The solution is to set the NSDateFormatter locale to en_US_POSIX.

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:locale];
[locale release];

by Luke Redpath on Stackoverflow.com

Android Toast-like HUD for iOS

If you wish to easily report status of background processes to the user, on Android you may use Toast class, however iOS lacks of this great feature. But there is a great library, that provides even more features than Android version: MBProgressHUD.

Adding settings bundle to iOS app

If you want to add your app’s settings to the main iOS settings page, there are some pretty easy and straightforward steps to do it, which is presented very well in the following tutorial.

“Real” URL encoding in iOS

Sometimes if you wish to encode a whole URL including /,&,: , [NSString stringByAddingPercentEscapes:] fails, to solve this issue, use CFURLCreateStringByAddingPercentEscapes

+ (NSString*)urlEncode: (NSString*) url {
    NSString* encoded = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)url, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);
    return [encoded autorelease];