Saturday, September 21, 2013

How to set custom font in iOS application.

Today i will show you how to set custom font for iOS application. As market grows much people require unique app for their business, fun and for other purpose, so customization is much require for app. Either image, unique UI, Fonts, or any photoshop design.While developing iOS app i also get similar kind of requirement for font customization. 

iOS apps are simply using the “System” font which is the font by default! so i am going to explain the solution for that is to add your custom fonts to your app.Use your existing app's Xcode project or you can create a new Xcode project. 

Follow the below simple steps:

1) Add your font (.TTF) files to your project in XCode  (The files should appear as well in “Target -> Build Phases -> Copy Bundle Resources”).


2) In your “Application-Info.plist” file, add the key “Fonts provided by application” with type “Array”.
3) For each font you want to add to your project, create an item for the array you have created with the full name of the file including its extension (e.g. myfont.ttf).
4) Save your “Application - Info.plist” file.


Usage of Font in App:

Add label in your view controller by simple code, it will display simple default font(Helvetica) in app.

UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, 50.0f, 200.0f, 50.0f)];
    lbl.backgroundColor = [UIColor clearColor];
    lbl.text = @"Hello World";
    [self.view addSubview:lbl];

Add below one line code and set the font which you wan't to 

lbl.font = [UIFont fontWithName:@"Fontname" size:20];

UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, 50.0f, 200.0f, 50.0f)];
    lbl.backgroundColor = [UIColor clearColor];
    lbl.text = @"Hello World";
    lbl.font = [UIFont fontWithName:@"rayando" size:20];
    [self.view addSubview:lbl];


that's it so simple to add font in app. now, suppose you wan't add change label in IB and wan't to use custom UILabel than what to do. Create Subclass of UILabel type and use of it will gives the solution. Add below code into subclass of the uilabel

Code of .h file 

#import <UIKit/UIKit.h>

@interface KTLabel : UILabel

@end


Code of .m file

#import "KTLabel.h"

@implementation KTLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


- (id)initWithCoder:(NSCoder *)decoder {
    
    if (self = [super initWithCoder: decoder]) {
        [self setFont: [UIFont fontWithName: @"rayando" size: self.font.pointSize]];
    }
    
    return self;
}


@end


here initWithCoder method of the NSCoder class is used to archive/unarchive of objects.This is a method to write objects on streams (like files, sockets) and being able to retrieve them later or in a different place.

You get custom fonts from  DaFont.(http://www.dafont.com/) and http://www.fontspace.com/



Monday, September 9, 2013

How to install CocoaPods & intro of cocoa pods



CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with CocoaPods. For get in to deep in cocoa pods you must have prior knowledge of the Xcode and command line Tool.

Why?

When working with third party libraries for iOS and OS X project, its hard to manage it manually. CocoaPods makes managing a project’s dependencies much easier. You only need to specify which dependencies, or pods, you want to include in your project and CocoaPods takes care of the rest.

How to install CocoaPods

Cocoapods runs on Ruby, to install & manage it easily. MAC OS X all version by default installed the Ruby. Before starting make sure you installed command line tool from Xcode preference menu. Refer the below image 1 for Xcode preference menu.


Image 1

Now you need to do is update RubyGems. For that open the Terminal from Launchpad > Utilities > Terminal on MAC. Type the following command in Terminal:

sudo gem update --system

Now, install cocoa pods by typing below command in Terminal

sudo gem install cocoapods
pod setup

Integrating Cocapods with an Xcode project

Create new Xcode project.  (Refer image 2)
Open Xcode -> File -> New -> Project - > Select Single View Application 


Image 2

After creating the project your project structure is look like below image (Refer image3).


Image 3 

Close the Xcode project.  Now we will go for install our first dependency for project. There are so many third party library available  on cocoa pods but for this tutorial we will use SVProgressHUD - A awesome library to create custom activity HUDs for iOS Application, More information about this library you will get here(https://github.com/samvermette/SVProgressHUD).

Now open terminal and change directory to your project directory. Enter below command in terminal:

vi Podfile

The simple editor open in command line add your dependency here. for example:

pod 'SVProgressHUD', '0.9'

A pod is keyword followed by the name  of library. You can add optional version number for library. Here '0.9' indicates the version no , if you left the version number empty CocoaPods will use the latest version available. Now hit the below command in terminal:

pod install

Common Error - Be careful Here.


If you didn't put your Podfile with your .xcodeproj file you will not get success to get the library via pod. Below error you will get (Refer image 4), so make sure your Podfile is with the .xcodeproj file.


Image 4

Upon success of the 'Pod install' you will get below result (Refer image 5).


Image 5

Your project directory structure is look like this (Refer image 6). Here you get the new .xcworkspace file. From now onwards we will use this file to open the project.


Image 6

if you not open the .xcworkspace and you open normal  .xcodeproj file, it will shows you below error on compilation. To avoid it just drag the Pods project into Xcode from your project directory structure. If you directly open the .xcworkspace then no need to drag & add the Pods into Xcode.

open MyCocoapods.xcworkspace to continue and test if it working or not. Thats it you get your third party library for your project. 

Now, suppose your project already contains the pod file and you wan't to add one more third party library in your project then what? Answer is very simple, browse to your pod file in project structure  in terminal. Edit the pod file by hitting vi command in terminal. Add your require dependancies and hit command again in terminal:

pod install

If  your third party library depends on another library, then pods will download and installed that dependent library too. So, thats too handy, no need to worry about dependencies. This make cocoa pods to easy to use for  developer.

Ref: