Tuesday, July 31, 2012

How to use NSAttributedString in iOS 6


infoString=@"This is an example of Attributed String";

NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];
NSInteger _stringLength=[infoString length];

UIColor *_black=[UIColor blackColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, _stringLength)];



               






UIColor *_red=[UIColor redColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:3.0] range:NSMakeRange(0, _stringLength)];















UIColor *_red=[UIColor redColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:-3.0] range:NSMakeRange(0, _stringLength)];

















UIColor *_red=[UIColor redColor];
UIColor *_green=[UIColor greenColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_green range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:-3.0] range:NSMakeRange(0, _stringLength)];



















UIColor *_green=[UIColor greenColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];

NSShadow *shadowDic=[[NSShadow alloc] init];
[shadowDic setShadowBlurRadius:5.0];
[shadowDic setShadowColor:[UIColor grayColor]];
[shadowDic setShadowOffset:CGSizeMake(0, 3)];
              
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_green range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSShadowAttributeName value:shadowDic range:NSMakeRange(0, _stringLength)];
                



















UIColor *_red=[UIColor redColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
  [attString addAttribute:NSForegroundColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];              
  [attString addAttribute:NSKernAttributeName value:[NSNumber numberWithInt:5] range:NSMak








UIColor *_red=[UIColor redColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0, _stringLength)];








UIColor *_blue=[UIColor blueColor];
UIColor *_blueL=[UIColor colorWithRed:0 green:0 blue:0.5 alpha:0.7];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
                
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_blue range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSBackgroundColorAttributeName value:_blueL range:NSMakeRange(0, 20)];


                

Thursday, July 26, 2012

Tips: Hide scrollbars of UIScrollView




[mScrollView setShowsHorizontalScrollIndicator:NO];
[mScrollView setShowsVerticalScrollIndicator:NO];


Tuesday, July 24, 2012

How to animate a line draw ?

If you want to draw a line pixel by pixel.
You will have to use combination of UIBezierPath, CAShapeLayer, CABasicAnimation. You can download the code here. The piece of code given below  animates a simple line drawn pixel by pixel from point (50.0,0.0to point (120.0600.0).


    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(50.0,0.0)];
    [path addLineToPoint:CGPointMake(120.0, 600.0)];




    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.frame = self.view.bounds;
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [[UIColor redColor] CGColor];
    pathLayer.fillColor = nil;
    pathLayer.lineWidth = 2.0f;
    pathLayer.lineJoin = kCALineJoinBevel;

    [self.view.layer addSublayer:pathLayer];

    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 2.0;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];




Saturday, July 21, 2012

convert NSArray in float[]


        NSArray *c=[NSArray arrayWithObjects:@"1",@"8",@"4",@"2",nil];
        
        NSEnumerator *enumerator;
        float * cArray;
        id obj;
        int index=0;
        
        cArray = (float *) malloc(sizeof(float) * [c count]);
        enumerator = [c objectEnumerator];
        while(obj = [enumerator nextObject])
        {
            cArray[index] = [obj floatValue];
            index++;
        }
        



       


Thursday, July 19, 2012

Ad Hoc Build in XCode 4.3+ and XCode 4.5

Things have changed a little bit for ad hoc. This post is for people who may face problem while trying to create adhoc. While trying to install .ipa on device through iTunes, it gives error "iTunes sync failed to start"

Make sure your mobile provisioning for ad-hoc and distribution certificate is installed before we proceed any further. You can go through my old post, it is very detailed, just to refresh things like how to create certificate and other required things.

Note: You can read captions on the image for more clarity.

Add Ad Hoc configuration by duplicating release (You can do that by pressing + sign))






















You need to add distribution signing profile for Ad Hoc as well as Release.

















Now on top left corner of your XCode screen. Click on project Name like here I click on MIMChartLIb

















I get popup like this.Click on Edit Scheme.

















Go to Archieve(Highlighted cell on left tableview). On right side,Change Build Configuration to Ad Hoc.Click OK.
































Now go to Product > Archive






















It will ask you to sign with keys. Click Allow or Always Allow.

















After sometime, Organiser window will open displaying Archive. If not, You can open Organiser from Xcode Menu: Window>Organiser.
































Clicking on Distribute button on previous screen, opens this. Choose second option here.
Next it asks for Code Signing Identity, Choose your Ad-Hoc distribution profile.
































It will ask you to sign with the key Click Allow or Always Allow.
































After sometime, it will ask you where to save your .ipa file. Thats it ! You select your location and click Save.
































Tuesday, July 17, 2012

Tutorial: How to use QLPreviewController to preview files.


This tutorial will talk about implementation of preview feature of documents, images and files available on iOS 4.0 onwards. Apple reference document says:


A Quick Look preview controller can display previews for the following items:
  • iWork documents
  • Microsoft Office documents (Office ‘97 and newer)
  • Rich Text Format (RTF) documents
  • PDF files
  • Images
  • Text files whose uniform type identifier (UTI) conforms to the public.text type (see Uniform Type Identifiers Reference)
  • Comma-separated value (csv) files


You need to add QuickLook.framework in your project. and import QuickLook/QuickLook.h
and add protocols <QLPreviewControllerDataSource,QLPreviewControllerDelegate>





QLPreviewController *previewController=[[QLPreviewController alloc]init];
previewController.delegate=self;
previewController.dataSource=self;
[self presentModalViewController:previewController animated:YES];
[previewController.navigationItem setRightBarButtonItem:nil];


You need to add datasource methods for sure. Return more than 1 for numberOfPreviewItemsInPreviewController if you want user to be able to preview next and previous file while in preview mode. Else return 1 if you want user to exclusively preview only selected file.
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index


You may add delegate methods  optionally depending on your requirements. In Sample code, I have used these delegate methods to add the zooming effect where the preview seems to come out of the same button clicked by user.



- (BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id <QLPreviewItem>)item
- (CGRect)previewController:(QLPreviewController *)controller frameForPreviewItem:(id <QLPreviewItem>)item inSourceView:(UIView **)view



For more information, you can view the Apple reference document.

Link of the sample code to download.

Make a difference with your App.

This is a great video  inspiring the developers to make an app which actually makes a difference !  LINK




Monday, July 16, 2012

Importance of SLComposeViewControllerCompletionHandler

You need to add the handler(SLComposeViewControllerCompletionHandler) otherwise you may get following error when you try to cancel the Facebook Post or post the Post:

viewServiceDidTerminateWithError:: Error Domain=_UIViewServiceErrorDomain Code =1 "The operation couldn't be completed. (_UIViewServiceErrorDomain error 1.)"


You need to create handler like following

SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result)
{
            
            [fbController dismissViewControllerAnimated:YES completion:nil];
            
            switch(result){
            case SLComposeViewControllerResultCancelled:
            default:
            {
                NSLog(@"Cancelled.....");
                
            }
                break;
            case SLComposeViewControllerResultDone:
            {
                NSLog(@"Posted....");
            }
                break;
        }};


and add this handler to SLComposeViewController


SLComposeViewController *fbController=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbController setCompletionHandler:completionHandler];

and you can see the last post to see how to use it completely with the sample code. LINK.



Tutorial: How to use inbuilt Facebook/Twitter API in iOS6.

You need to add Social.framework in your project.
Add to your file following #import "Social/Social.h"


You can create 3 type of service:



SLServiceTypeTwitter;
SLServiceTypeFacebook;
SLServiceTypeSinaWeibo;

I have created service of Facebook.


SLComposeViewController *fbController=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];


if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
        SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
        
        [fbController dismissViewControllerAnimated:YES completion:nil];
        
        switch(result){
        case SLComposeViewControllerResultCancelled:
        default:
        {
            NSLog(@"Cancelled.....");
            
        }
            break;
        case SLComposeViewControllerResultDone:
        {
            NSLog(@"Posted....");
        }
            break;
    }};
    
    [fbController addImage:[UIImage imageNamed:@"1.jpg"]];
    [fbController setInitialText:@"Check out this article."];
    [fbController addURL:[NSURL URLWithString:@"http://soulwithmobiletechnology.blogspot.com/"]];
    [fbController setCompletionHandler:completionHandler];
    [self presentViewController:fbController animated:YES completion:nil];
}



I have added image which will be posted with the message and URL on the Facebook page. Here is what the message looks like on FB.









Following are some screenshots of the app running.


If the Facebook account is not set up on device, it gives you this pop up.

SLComposeViewController with added URL and Image.

SLComposeViewController with added text,URL and image,
Also location has been set on the post
just like we can do on our Facebook page.

You can change the album in which you want to post this image.

You can choose which group you want to display this post to.





Link to download the sample code.

Thursday, July 12, 2012

Mac OS Evolution

1978 Apple introduces Apple DOS 3.1, the first operating system for the Apple computers in June.

1984 Apple introduces System 1.

1985 Apple introduces System 2.

1986 Apple introduces System 3.

1987 Apple introduces System 4.

1988 Apple introduces System 6.

1991 Apple introduces System 7 operating system May 13, 1991.

1995 Apple allows other computer companies to clone its computer by announcing its licensed the Macintosh operating system rights to Radius on January 4.

1997 Apple introduces Mac OS 8. 1997Apple buys NeXT Software Inc. for $400 million and acquires Steve Jobs, Apples cofounder, as a consultant.

1999 Apple introduces Mac OS 9.

2001 Apple introduces Mac OS X 10.0 named Cheetah and becomes available March 24, 2001.

2001 Apple introduces Mac OS X 10.1 named Puma and becomes available on September 25, 2001.

2002 Apple introduces Mac OS X 10.2 named Jaguar and becomes available on August 23, 2002.

2003 Apple introduces Mac OS X 10.3 named Panther October 25, 2003.

2004 Apple introduces Mac OS X 10.4 named Tiger at the WWDC on June 28, 2004.

2007 Apple introduces Mac OS X 10.5 named Leopard October 26, 2007.

2008 Apple introduces Mac OS X 10.6 named Snow Leopard and MobileMe at the WWDC on June 9, 2008.

2011 Apple introduces Mac OS X 10.7 named Lion was released on July 20, 2011.

2012 Apple introduces Mac OS X 10.8 named Mountain Lion will be released  in July 2012.

Tuesday, July 10, 2012

Features in new Mountain Lion OS.

Atmosphere is again hot with the anticipation of new Mac OS. Again, millions of 20 dollars will be added to trillion dollar Apple :) Lets take a quick look at the  feature list.

  •  iCloud is automatic and effortless now. Just sign in once with your Apple ID and iCloud is set up in all the apps that use it.

  • Reminder:  Set due dates and you’ll get alerts as deadlines approach. Set a location from your Mac, and your iPhone or iPad will remind you when you get there.

  • Notes: Its more noteworthy with photos, images, and attachments.Pin important notes to your desktop so they’re easy to get to. 

  • iMessage: You can send messages to anyone on an iPhone, iPad, or iPod touch running iOS 5, too.Send photos, videos, documents, and contacts — even send messages to a group. 

  •  Notification Center just like iPad—for  any new email, a message, a software update or a calendar alert.

  • Power Nap is most awesome-It periodically updates Mail, Contacts, Calendar, Reminders, Notes, Photo Stream, Find My Mac, and Documents in the Cloud,downloads software updates and makes backups with Time Machine. While updating, the system sounds are silent and no lights or fans come on, so nothing disturbs you as reported on Apple's website.

  • Dictation -It converts your words into text.And it recognizes people from your contacts so it enters names accurately. Dictation supports English (U.S., UK, and Australia), French, German, and Japanese.

  • Share Button-There is Share button throughout OS X Mountain Lion.

  • Built-in Facebook support- You can share what’s up with you right from the app you’re in. Post photos or links. Add comments and locations.OS X adds your Facebook friends and their profile photos to Contacts.

  • Built-in Twitter support- OS X Mountain Lion is designed for tweeting. Sign in once and you’re all set up .You can tweet from Notification Center, too.

  • Game Center app - You can play anyone on a Mac, iPad, iPhone, and iPod touch.Just use your Game Center account from iOS or create one with your Apple ID. Then sign in and you’re in.

  • Gatekeeper -It helps protect you from downloading and installing malicious software on your Mac.

  • Safari- When you open web pages on your iPhone or iPad, iCloud Tabs makes them available on your Mac, too. So you can pick up browsing wherever you left off.  Safari saves entire web pages in your Reading List,not just the links. 

Running on iPhone or iPad ?

Developing a universal app makes it a necessity to find out if the app is running on iPhone or iPad, accordingly you write your code block.
Here is the code:


if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
                
}
                
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
                
}

How to find OS version of iOS device.


NSString *currentOS = [[UIDevice currentDevice] systemVersion];



How to check if current OS is minimum version you need. Suppose minimum you need is 4.2

NSString *currentOS = [[UIDevice currentDevice] systemVersion];
if ([currentOS compare:@"4.2" options:NSNumericSearch] != NSOrderedAscending)
{
     NSLog(@"Yes, it is greater than or equal to 4.2");
}



                

Monday, July 2, 2012

How to add Event in device Calender.


        EKEventStore *eventStore = [[EKEventStore alloc] init];
        
        EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
        event.title     = @"My Event Name";
     
        event.startDate =[NSDate date];
        event.endDate   = [[NSDate alloc] initWithTimeInterval:60 sinceDate:event.startDate];
       
        [event setCalendar:[eventStore defaultCalendarForNewEvents]];
        NSError *err;
        [eventStore saveEvent:event span:EKSpanThisEvent error:&err];  



And don't forget to add #import  to your file and EventKit framework to your project.