Wednesday, 9 April 2014

Calculating Height of UITextView in iOS7



Many times in application we need to calculate height of UITextView to make it grow and shrink according to the text contained within it.

Now many of you will say calculating height of UITextView is not a big deal.

Yes its true for iOS6 and below version. But not true from iOS7 and higher.

In iOS6 we can get height of a UITextView simply using the contentSize.

eg.

  [myTextView setText:@"here goes my text"];

  CGFloat heightOfTextView = myTextView.contentSize.height;


In iOS7 may be it's a bug or due to deprecation, contentSize does not provide you the accurate height of the UITextView.

So below is the code snippet i used in my application for getting the height of UITextView:

Just set text NSString to UITextView and call this method sending UITextView as parameter


  [myTextView setText:@"here goes my text"];

  [self measureHeightOfUITextView: myTextView];


- (CGFloat)measureHeightOfUITextView:@"here goes my text"];​(UITextView *)textView

{

    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)

    {

        // This is the code for iOS 7. contentSize no longer returns the correct value, so

        // we have to calculate it.

        CGRect frame = textView.bounds;

        // Take account of the padding added around the text.​

        UIEdgeInsets textContainerInsets = textView.textContainerInset;

        UIEdgeInsets contentInsets = textView.contentInset;

        CGFloat leftRightPadding = textContainerInsets.left + textContainerInsets.right + textView.textContainer.lineFragmentPadding * 2 + contentInsets.left + contentInsets.right;

        CGFloat topBottomPadding = textContainerInsets.top + textContainerInsets.bottom + contentInsets.top + contentInsets.bottom;

        frame.size.width -= leftRightPadding;

        frame.size.height -= topBottomPadding;

        NSString *textToMeasure = textView.text;

        if ([textToMeasure hasSuffix:@"\n"])

        {

            textToMeasure = [NSString stringWithFormat:@"%@-", textView.text];

        }       

        // NSString class method: boundingRectWithSize:options:attributes:context is

        // available only on ios7.0 sdk.

        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

        [paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];

        NSDictionary *attributes = @{ NSFontAttributeName: textView.font, NSParagraphStyleAttributeName : paragraphStyle };

        CGRect size = [textToMeasure boundingRectWithSize:CGSizeMake(CGRectGetWidth(frame), MAXFLOAT)

                                                  options:NSStringDrawingUsesLineFragmentOrigin

                                               attributes:attributes

                                                  context:nil];

        CGFloat measuredHeight = ceilf(CGRectGetHeight(size) + topBottomPadding);

        return measuredHeight;

    }

    else

    {

        return textView.contentSize.height;

    }

}



I got help from below links for above code snippet:
Link 1
Link 2

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) {

                    }];

}