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/



1 comment:

  1. However with this prerelease adaptation, we were just equipped to look at fundamental peculiarities, including a demo reel while we wore the watch on our own wrists, and working gadgets worked by Apple workers to demonstrate to us more exceptional usefulness. We will have same ios 8 app developers in our apply.

    ReplyDelete