Tuesday 7 August 2012

Five Tips for Creating Stylish UIButtons

Source: http://mobile.tutsplus.com

 

Project Preview

Project Demonstration

Project Setup

In the download that accompanies this tutorial, you’ll find folders entitled “Initial Build” and “Final Build”. Rather than showing all the steps necessary to setup the initial project, you should simply download the attachment and follow along using the project from the “Initial Build” folder.
With the initial project open, go to the ViewController.m file and locate the for loop within the viewDidLoad method. The code snippets from the tips below should be placed within this loop.

Tip #1: Tweak Colors & Gradients

The most fundamental step toward customizing the UIButton class is to adjust the color of the background and title for both the default and highlighted states. The following code snippet sets each button’s background color to black, normal title color to white, and highlighted title color to red:
1
2
3
4
5
6
// Set the button Text Color
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
         
// Set the button Background Color
[btn setBackgroundColor:[UIColor blackColor]];
Solid background colors are great, but a subtle gradient can often make the difference between bland and polished. Replace the setBackgroundColor: message above with the following to create a custom gradient:
1
2
3
4
5
6
7
8
// Draw a custom gradient
CAGradientLayer *btnGradient = [CAGradientLayer layer];
btnGradient.frame = btn.bounds;
btnGradient.colors = [NSArray arrayWithObjects:
                              (id)[[UIColor colorWithRed:102.0f / 255.0f green:102.0f / 255.0f blue:102.0f / 255.0f alpha:1.0f] CGColor],
                              (id)[[UIColor colorWithRed:51.0f / 255.0f green:51.0f / 255.0f blue:51.0f / 255.0f alpha:1.0f] CGColor],
                             nil];
[btn.layer insertSublayer:btnGradient atIndex:0];
Starting on line 4 above, an NSArray is created with the initial and target gradient colors. Note that the corresponding RGB values must be divided by 255 before being supplied to the colorWithRed:green:blue:alpha: message and that an alpha value of 1.0 represents fully opaque while an alpha value of 0.0 represents fully transparent. Unfortunately, a full explanation of the “magic” above is beyond the scope of this tutorial, but the important thing to remember is to that you simply need to replace the RGB values with the begin/end values you want to use in your own custom gradient.
If all went well, your menu should now look something like this:
Custom Color and Gradient
Not bad, huh? And we’ve only just begun. . .

Tip #2: Round the Corners

Next we want to add a custom corner radius to each UIButton in order to make things look a bit more sleek. Insert the following lines of code to make this happen:
1
2
3
4
// Round button corners
CALayer *btnLayer = [btn layer];
[btnLayer setMasksToBounds:YES];
[btnLayer setCornerRadius:5.0f];
In line 4 above the corner radius is set to 5.0. Play with this number to increase or decrease how noticeable the corners appear.
You should now have a menu that looks just a bit slicker:
Adding Corner Radius

Tip #3: Add a Stroke Border

Sometimes the small tweaks make all the difference. Add a 1px, black stroke around each button with the following lines of code:
1
2
3
// Apply a 1 pixel, black border
[btnLayer setBorderWidth:1.0f];
[btnLayer setBorderColor:[[UIColor blackColor] CGColor]]; 
Can you tell the difference? It’s very subtle, but still valuable:
Adding A 1px Border

Tip #4: Use a Custom Font

Now let’s try a more noteworthy tweak. The default system font just isn’t cutting it. The game menu we’re building needs a font that can match the visual aesthetic of the game. A quick search on Google Fonts reveals just a font called Knewave by Tyler Finck that should do the trick. Download Knewave now.
After downloading the Knewave-Regular.ttf file, you’ll need to drag it into the Project Navigator pane in Xcode to add it to your project. Next, open up the Info.plist file. Add a new property list row and type in “Fonts provided by application”. An array should be created automatically. Set the string associated with Item 0 to “Knewave-Regular.ttf”. Double check the name because the value is case sensitive. Save the file.
After making the above modification, your Info.plist file should now look like this:
Plist Entry
Next, you’ll need to add the Knewave-Regular.ttf file to your project’s bundled resources. Select “SleekButtons” from the Project Navigator and then click the “Build Phases” tab. Expand the “Copy Bundle Resources” drop down and then click the plus sign.
Adding to Bundled Resources
At this point, you should be able to begin using the Knewave font in your project! Let’s test that out by jumping back to the ViewController.m file and modifying the viewDidLoad method to set a custom font:
1
2
3
4
5
6
7
8
9
10
// Set the button Text Color
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
               
// Add Custom Font
[[btn titleLabel] setFont:[UIFont fontWithName:@"Knewave" size:18.0f]];
         
// Draw a custom gradient
CAGradientLayer *btnGradient = [CAGradientLayer layer];
btnGradient.frame = btn.bounds;
Notice that the fontWithName value is specified as “Knewave”, not “Knewave-Regular” as you might expect. This is because there is a difference between the font’s filename and the font’s given name. You’ll need to be sure you use the given name when working with your own fonts.
With the above code in place, the game menu should be complete! Build and run now and you should see something like the following:
Custom Fonts

Tip #5: Optional: Apply a Rotation

While not utilized in the primary design demonstrated by this tutorial, it’s often useful to apply a slight rotation to UIKit elements, particularly UIButton or UIImage objects. Doing so is simple, and can be done with just one line of code.
You can try this out with the code written so far by doing the following:
1
2
3
4
5
6
7
self.startGameButton.transform = CGAffineTransformMakeRotation(M_PI / 180 * 5.0f);
     
for(UIButton *btn in buttons)
{
    // Set the button Text Color
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
In line 1 above, the constant M_PI is divided by 180 to generate 1 radian, which is then multiplied by 5.0f to result in a rotation of 5 radians. As you will notice if you build and run the project, the above is likely to result in anti-aliasing problems with UIKit elements that are drawn dynamically. Consequently, applying a rotation is more appropriate with a UIButton when the background image property is set and raster graphics are in use (i.e. this works best with PNG files).

Bonus Tip: Use UIButton-Glossy

With the five tips above, you’ve seen how easy it can be to make subtle yet significant tweaks to a UIButton object. However, what if I told you that doing so could be even easier? Meet UIButton-Glossy, an open-source category by George McMullen that automatically applies both a gradient and a glossy finish to UIButton objects.
Implementing UIButton-Glossy in your own project is simple. After you’ve downloaded the UIButton-Glossy.h and UIButton-Glossy.m files and added them to your Xcode project, import the *.h file in the main project view controller, like this:
1
2
#import "ViewController.h"
#import "UIButton+Glossy.h"
Now you can instantly apply a cool glossy effect on your buttons with just one line of code: [btn makeGlossy];.
To see this in action, replace the existing view controller code with the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for(UIButton *btn in buttons)
{
     // Set the button Text Color
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
         
    // Set default backgrond color
    [btn setBackgroundColor:[UIColor blackColor]];
         
    // Add Custom Font
    [[btn titleLabel] setFont:[UIFont fontWithName:@"Knewave" size:18.0f]];
         
    // Make glossy
    [btn makeGlossy];
}
Your buttons should now have a glossy finish and the end result should look something like this:
Glossy Finish

Saturday 4 August 2012

10 Things You Never Knew About the Olympic Games


Today marks the Opening Ceremony of the 2012 Summer Olympic Games, and here at StumbleUpon, we couldn’t be more excited. Hopefully you’ve also been getting into the competitive spirit with our posts earlier this week on fitness myths and nutrition advice. We decided to honor today’s festivities with 10 unusual facts that you may or may not know about the Olympic Games, from the past to the present.
1. Every Olympics has an official mascot, and they’re not your typical eagles, tigers or bulldogs. In fact, most of them are rather odd. This year’s mascots, Wenlock and Mandeville, meant to represent the last drops of steel used to build the Olympic Stadium, are no exception.
2. Usain Bolt is a proud papa…to a cheetah! Bolt adopted an abandoned baby cheetah (fittingly named “Lightning Bolt”) in 2009, and continues to sponsor its care in an orphanage in Nairobi. Hmm…wonder who’s faster?
3. What do Giorgio Armani, Adidas and Stella McCartney have in common? They’ve all designed national team uniforms for the Opening Ceremony. Sporting everything from berets to fedoras, this year’s athletes will certainly be dressed to impress.
4. History will be made this year when double-amputee Oscar Pistorius runs for South Africa in the individual 400 meters and the 4×400 meter relay. Pistorius will be the first amputee ever to compete in track and field at the Olympics.
5. Hopefully he’ll fare better than Charles Hefferon, a fellow South African runner who competed in the Marathon in the 1908 Summer Olympics. Hefferon was in the lead until, with just a few miles to go, he drank a glass of champagne offered to him by a well-meaning fan. He promptly slowed down and was overtaken by other runners. Lesson learned: don’t drink and run!
6. The official song of London 2012 is “Survival,” by popular British band Muse. Check it out here (along with a cool montage video of Olympic athletes).
7. Are moustaches lucky? The night before his first race at the 1972 Olympic Games, legendary swimming champ Mark Spitz almost decided to shave off his famous moustache. However, a Russian coach started giving him a hard time about sporting facial hair, so Spitz joked that the moustache made him swim faster by keeping water away from his mouth. Spitz kept the ‘stache, went on to win 7 gold medals at the Games, and at the next Olympics, all the Russian swimmers were sporting moustaches.
8. The iconic Olympic rings were designed to symbolize the five inhabited continents of the world (Africa, Asia, Australia, Europe and the Americas), linked together by the Olympic spirit. At least one of the rings’ colors (blue, yellow, black, green and red, with a white background) are present in every participating country’s national flag.
9. The longest wrestling match of all time took place at the 1912 Stockholm Olympics, between Russia’s Martin Klein and Finland’s Alfred Asikainen. The match lasted a grueling 11 hours and 40 minutes! Klein eventually emerged the victor, but he was so drained from the fight that he was unable to compete for the gold medal the next day, and walked away with the silver.
10. You may still not know what the heck curling is, but that’s nothing compared to some of the very strange sports that have made it into the Olympics in the past. Our personal favorites? Definitely poodle clipping and delivery van driving.