Monday, 31 March 2014

Fetch list of fonts and font family in iPhone programmatically using XCode. Steps to add Custom Fonts in xcode. Fetch list of Custom fonts added to app.

Fetch list of Default fonts.

-(voidfetchDefaultFonts
{
    // [UIFont familyNames] returns the array of available fonts family
    for (id familyName in [UIFont familyNames]) {
        NSLog(@"%@", familyName);
        // [UIFont fontNamesForFamilyName:familyName] returns the array of available fonts that fontFamily
        for (id fontName in [UIFont fontNamesForFamilyName: familyName]){
             NSLog(@"%@", fontName);
        }
    }
}

iOS 5 fonts
iOS 6 Fonts



Fetch list of Custom fonts added to app.

NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSArray* fontFiles = [infoDict objectForKey:@"UIAppFonts"];

for (NSString *fontFile in fontFiles) {
    NSLog(@"file name: %@", fontFile);
    NSURL *url = [[NSBundle mainBundle] URLForResource:fontFile withExtension:NULL];
    NSData *fontData = [NSData dataWithContentsOfURL:url];
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData);
    CGFontRef loadedFont = CGFontCreateWithDataProvider(fontDataProvider);
    NSString *fullName = CFBridgingRelease(CGFontCopyFullName(loadedFont));
    CGFontRelease(loadedFont);
    CGDataProviderRelease(fontDataProvider);
    NSLog(@"font name: %@", fullName);
}

Steps to add Custom Fonts
Add by draging your custom font file(Eg: "myFont-Test.ttf") in XCode project.
1.) Open your project "YourApp-info.plist" under "Supporting Files" folder.
2.) Add new row in the list with key as "Fonts provided by application"or"UIAppFonts".
3.) In this array add your fonts with full name. Eg: item 0: "myFont-Test.ttf"
4.) Use in your new font in code as
[UIFont fontWithName:@"myFont-Test" size:16.0];




Sunday, 30 March 2014

'NSAutoreleasePool' is unavailable: not available in automatic reference counting mode

'NSAutoreleasePool' is unavailable: not available in automatic reference counting mode

 'release' is unavailable: not available in automatic reference counting mode

ARC forbids explicit message send of 'release'

These all above 3 errors can occurs while using "NSAutoreleasePool" with "ARC" on

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
    NSLog (@"hello, this old way of using autorelease pool!!");
    [pool drain];
    return 0;

Change above code to below code

    @autoreleasepool {
    NSLog (@"hello, this is new way of using autorelease pool with arc!!");
     }

Thursday, 27 February 2014

NSDate few year ago from today. NSDate to future or past date.

    
    NSDate *today = [NSDate date];
    NSLog(@"today: %@", today);
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    [offsetComponents setYear:-18]; // setting it to -18 (near 18 years ago,from today, +18 will shift to future date)
//    [offsetComponents setMonth:-1];
//    [offsetComponents setDay:-1];
    
    NSDate *newShiftedDate = [gregorianCalendar dateByAddingComponents: offsetComponents toDate: today options:0];

    NSLog(@"newShiftedDate: %@", newShiftedDate);

Thursday, 30 January 2014

Transition like app launch in iOS 7.. Show and hide view as the app open up in iOS 7..

//Show animation-------
loginViewController.view.frame = CGRectMake(0, 0, self.window.frame.size.width, self.window.frame. size.height);
loginViewController.view.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
loginViewController.view.layer.anchorPoint = CGPointMake(0, 0);
loginViewController.view.layer.position = CGPointMake(0, 0);
[UIView animateWithDuration:0.6f delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
loginViewController.view. transform = CGAffineTransformMakeScale(1.0, 1.0);
} completion:^(BOOL finished){

}];



//Hide animation-------
loginViewController.view.layer.anchorPoint = CGPointMake(0, 0);
loginViewController.view.layer.position = CGPointMake(0, 0);
[UIView animateWithDuration:0.6f delay:0 options:UIViewAnimationOptionAllowUserInteraction   animations:^{
// loginViewController. view.alpha = 0.0f;
loginViewController. view. transform = CGAffineTransformMakeScale(0.0001, 0.0001);
} completion:^(BOOL finished)
{
//NSLog(@"sliding finished...");
[loginViewController removeFromParentViewController];
[loginViewController. view removeFromSuperview];

}];


Change the anchor point and position, to try different animations..



Friday, 24 January 2014

Decrease the clickable area of the bar buttons in navigation bar.

Some the you need to add the buttons just below UINavigationbar, but the UIBarButton on navbar click area interfere with your custom button click area.





I added hitTest:withEvent:  code to my  custom UINavigationbar subclass..


-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
            int errorMargin = 5;// space left to decrease the click event area
            CGRect smallerFrame = CGRectMake(0 , 0 - errorMargin, self.frame.size.width, self.frame.size.height);
            BOOL isTouchAllowed =  (CGRectContainsPoint(smallerFrame, point) == 1);
           
            if (isTouchAllowed) {
                self.userInteractionEnabled = YES;
            } else {
                self.userInteractionEnabled = NO;
            }
            return [super hitTest:point withEvent:event];
        }
}

Read More