Pages

Wednesday, May 25, 2011

[Code Snippet] Custom UINavigationItem title

Last week I was working in an iPhone navigation based project, the customer wanted to have its own corporative colors in the application, and for the UINavigationBar too.

There was no problem with whole color for buttons and bar, using the UINavigationBar tintColor proprerty I set the desired color, but when the UINavigationItem title time becomes it was more difficult. We have to forget the UINavigationItem title proprerty and work with titleView one. This proprerty can be fulfilled with any UIView object, in my case I used a UILabel, but I have to set a different titleView for my different UIViewControllers so I encapsulated this in the function that I make available for you.

+ (UILabel*) labelForNavigationItem:(NSString*)text
{
   
   
    UILabel *titleView = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)] autorelease];
    [titleView setFont:[UIFont boldSystemFontOfSize:16.0]];
    titleView.backgroundColor = [UIColor clearColor];
    titleView.textAlignment = UITextAlignmentCenter;
    titleView.textColor  = [UIColor colorWithRed:.80784314 green:.77647079 blue:.50980392 alpha:1.0];
    titleView.textColor = [UIColor whiteColor];
    titleView.text = text;
   
    return titleView;
}
And assign the corresponding proprerty on viewDidLoad function

self.navigationItem.titleView = labelForNavigationItem:@"Menu";
That's all.

But at last I found a problem that I wasn't able to accomplish. I want to keep the standard UINavigationController backButton, but how could I change its font color? I think its no possible (at least without using an image), but if someone has any answer I would be grateful.

Enjoy it!

Wednesday, May 18, 2011

[Code Snippet] Hex Color (HTML) to "iOS RGB"

Most of the available applications at mobile markets are games or marketing apps, so our customers wants a very visual interface, an interface which attract the potential users or only embed the corporation logo and colours into the app's look & feel.

But most of developers have one handicap: We don't know about graphic design, color combination and logo designs, although some customer don't take care about that, so we have to work with a designer which makes all that graphic work. In most cases the designer give us the colors in RGB mode (0-255), so we have to convert them to "Mac RGB" (0 to 1) dividing each value between 255. But, what happends when the designer give us the colors on hexadecimal (HTML) coding?.

Today's code snippet tries to solve that with this little function that you can use to convert from hexadecimal to "MAC RGB" whith only a call.


+ (UIColor *) colorWithHexString: (NSString *) stringToConvert
{
NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

// String should be 6 or 8 characters
if ([cString length] < 6) return DEFAULT_VOID_COLOR; // strip 0X if it appears if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2]; if ([cString length] != 6) return DEFAULT_VOID_COLOR; // Separate into r, g, b substrings NSRange range; range.location = 0; range.length = 2; NSString *rString = [cString substringWithRange:range]; range.location = 2; NSString *gString = [cString substringWithRange:range]; range.location = 4; NSString *bString = [cString substringWithRange:range]; // Scan values unsigned int r, g, b; [[NSScanner scannerWithString:rString] scanHexInt:&r]; [[NSScanner scannerWithString:gString] scanHexInt:&g]; [[NSScanner scannerWithString:bString] scanHexInt:&b]; return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f]; }




I stole this piece of code from this Ars Tecnica's article where you could find more about UIColor conversions in.

Enjoy it!

Monday, May 16, 2011

Follow us in Twitter.

Five Flames Mobile has its own profile in Twitter network. Follow us! (if you want)

Seguir a FiveFlames en Twitter

Sunday, May 15, 2011

[WindowsPhone-Game] Race

Continuing with the retro games serie for Windows Phone, this game tries to recreate that old arcade machine in which you are a driver who has to avoid those kamikaze cars that comes against you (or maybe we are the kamikaze) moving to the left or to the right, and pick up the girl that appears near the road's border.
Developed with XNA Framework for Windows Phone 7 this game is only available at Microsoft Marketplace, and maybe will be available soon for iPhone at appStore.

Name: Race
Language: N/A
Category: Game
Cost: 0,99$ / 0,79 €

Wednesday, May 11, 2011

[Code Snippet] UIActivitiyIndicator at right bar button place.

Let's continue with the weekly code snippet series. Sometimes we have an UITableViewController inside a UINavigationController that we want to update performing the data query in background and notify the user that this update is going on with a UIActivityIndicator control.

The better place to place the UIActivity indicator is on navigation bar, so the activity indicator don't appears in the middle of the screen bothering the user in is normal interaction with the application.

To accomplish this, here you are a little code snipe for placing an little UIActivityIndicator at top right corner of the screen instead of a UIBarButton.

// Create a 'right hand button' that is a activity Indicator
CGRect frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
self.activityIndicator = [[UIActivityIndicatorView alloc]
            initWithFrame:frame];
[self.activityIndicator sizeToFit];
self.activityIndicator.autoresizingMask =
    (UIViewAutoresizingFlexibleLeftMargin |
    UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleTopMargin |
    UIViewAutoresizingFlexibleBottomMargin);

UIBarButtonItem *loadingView = [[UIBarButtonItem alloc]
         initWithCustomView:self.activityIndicator];
loadingView.target = self;
self.navigationItem.rightBarButtonItem = loadingView;

I've found that code snippet in Even Davey's Blog

Enjoy it!

Wednesday, May 4, 2011

[Code Snippet] UITableViewCell background color

Let's go with the code snippet for this week. Recently I had to load some data in a UITableView controller and mark some cells with a different background color depending of its priority, so I tried to set the background color programatically in the cellForRowAtIndexPath method but nothing happends.

So looking for the internet I found this little code snippet that allows us to change UITableViewCell background color programatically.

UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
bg.backgroundColor = [UIColor greenColor]; // or any color
cell.backgroundView = bg;
[bg release];

Enjoy it!

[iPhone-app] CarCharge

CarCharge is an iPhone application that allows the user to localize the nearest electric vehicle (EV) charge point at Spain.
It allows user for searching in a distance from users location an shows all available charge points and the distance to them.
With a very visual and intuitive interface it will be a neccesary application for every EV owner who has an iPhone device.

Name: Carcharge
Language: N/A
Category: Lifestyle
Cost: FREE

CarCharg AppStore Link