Wednesday, 26 March 2014

Hyperlinks in iOS App by detecting Links, Phone Number, Address and CalendarEvent in UITextView


In many scenarios we have to detect Links(URLs) and Phone Numbers from given NSString and make it similar to hyperlink in HTML.
Suppose you have a string with multiple url's and you want to make it clickable, so as to be able to open it in Safari browser.

In below code i have taken a string which consists of two url's and i want to make it clickable.
So i take a UITextView "txtViewClickableHyperLink" and assigned the NSString "strText" to it.
Now i have used a magic line to setDataDetector of UITextView, this line has power to recognise my url.
By changing type of DataDetector we can recognise:
UIDataDetectorTypeLink                  - URL (Link)
UIDataDetectorTypePhoneNumber   - Phone Number
UIDataDetectorTypeAddress             - Address
UIDataDetectorTypeCalendarEvent   - Calendar Event
UIDataDetectorTypeAll                      - Detects all types

NSString *strText = [NSString stringWithFormat: @"Hey this is my blog: http://objectivecwithsuraj.blogspot.in/ which is hosted on https://www.blogger.com/. This is my blog here is share my experiences with all of you. So it can help others as well as me in future. "];
UITextView *txtViewClickableHyperLink = [[UITextView alloc] init];
[txtViewClickableHyperLink setFrame:CGRectMake(X_COORDINATE, Y_COORDINATE, WIDTH, HEIGHT)];
[txtViewClickableHyperLink setEditable:NO];
[txtViewClickableHyperLink setDataDetectorTypes:UIDataDetectorTypeLink];
[txtViewClickableHyperLink setText:strText];
[self.view addSubview:txtViewClickableHyperLink];

Now we can detect url, phone number , address and make it clickable using a UITextView in iPhone App.

Sample Code : HERE








Friday, 21 March 2014

UIPanGestureRecognizer to create Screen similar to Notification Center


In my application i wanted a Screen similar to the Notification Center of the iPhone.

Where user can navigate through 3 screens using swipe gesture and also by selecting the UISegmentedControl index.

So i have figured out below code.

 

-(void)viewDidLoad {

[super viewDidLoad];

 

// I have added a ScrollView on Xib file of width 960px here.

// added content(UIControls) required on first screen at 0px to 320px.

// added content(UIControls) required on second screen at 320px to 640px.

// added content(UIControls) required on third screen at 640px to 960px.

 

// I have added a UISegmentedControl on Xib file and set an action: - (IBAction)changeTab:(id)sender

 

// Add PangestureRecognizer to view

   UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeThroughTabs:)];

   [panRecognizer setMinimumNumberOfTouches:1];

   [panRecognizer setMaximumNumberOfTouches:1];

   [panRecognizer setDelegate:self];

   [self.view addGestureRecognizer:panRecognizer];

}

 

 

-(void)swipeThroughTabs:(id)sender {

   [[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];

   

   CGPoint velocity = [(UIPanGestureRecognizer*)sender velocityInView:[sender view]];

   

   if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

 

       if (velocity.x < 0) {

           if (segmentedControlForTab.selectedSegmentIndex == 0) {

               [self changeTabManually:1];

           } else if (segmentedControlForTab.selectedSegmentIndex == 1) {

               [self changeTabManually:2];

           }

       } else {

           if (segmentedControlForTab.selectedSegmentIndex == 2) {

               [self changeTabManually:1];

           } else if (segmentedControlForTab.selectedSegmentIndex == 1) {

               [self changeTabManually:0];

           }

       }

       

   }

}

 

-(void)changeTabManually:(NSInteger )selectedIndex {

   [self hideKeyboard];

   [UIView animateWithDuration:0.5

                         delay:0.0

                       options:UIViewAnimationOptionCurveEaseIn

                    animations:^ {

                        [segmentedControlForTab setSelectedSegmentIndex:selectedIndex];

                        CGRect frameOfScrollView = scrollViewSettingPage.frame;

                        switch (selectedIndex) {

                            case 0: {

                                frameOfScrollView.origin.x = 0;

                                scrollViewSettingPage.frame = frameOfScrollView;

                            }

                                break;

                               

                            case 1: {

                                frameOfScrollView.origin.x = -320;

                                scrollViewSettingPage.frame = frameOfScrollView;

                            }

                                break;

                               

                            case 2: {

                                frameOfScrollView.origin.x = -640;

                                scrollViewSettingPage.frame = frameOfScrollView;

                            }

                                break;

                               

                            default:

                                break;

                        }

                    }

                    completion:^(BOOL finished) {

                    }];

}

 

- (IBAction)changeTab:(id)sender {

   [self hideKeyboard];

   UISegmentedControl *segmentedControl = (id)sender;

   

   [UIView animateWithDuration:0.5

                         delay:0.0

                       options:UIViewAnimationOptionCurveEaseIn

                    animations:^ {

                        CGRect frameOfScrollView = scrollViewSettingPage.frame;

                        switch (segmentedControl.selectedSegmentIndex) {

                            case 0: {

                                frameOfScrollView.origin.x = 0;

                                scrollViewSettingPage.frame = frameOfScrollView;

                            }

                                break;

                               

                            case 1: {

                                frameOfScrollView.origin.x = -320;

                                scrollViewSettingPage.frame = frameOfScrollView;

                            }

                                break;

                               

                            case 2: {

                                frameOfScrollView.origin.x = -640;

                                scrollViewSettingPage.frame = frameOfScrollView;

                            }

                                break;

                               

                            default:

                                break;

                        }

                    }

                    completion:^(BOOL finished) {

                    }];

}