Wednesday, 1 October 2014

Set Multiple Images to ScrollView with PageControl

This is the code that I use when setting up a UIScrollView to contain multiple images that can be scrolled through. It has PageControl which shows no. of image as well.

Call this method from your Desired method (viewDidLoad or  viewWillAppear)

-(void) setupScrollView
 {
    //add the scrollview to the view

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
                                                                     self.view.frame.size.width,
                                                                     self.view.frame.size.height)];
    self.scrollView.pagingEnabled = YES;

    [self.scrollView setAlwaysBounceVertical:NO];

    //setup internal views
    NSInteger numberOfViews = 3;

    for (int i = 0; i < numberOfViews; i++) 
{
        CGFloat xOrigin = i * self.view.frame.size.width;

        // add PageControl

        self.pageControl = [[UIPageControl alloc] init];

        self.pageControl.frame = CGRectMake(xOrigin+120, 175, 90, 37);

        self.pageControl.numberOfPages = 5;

        self.pageControl.pageIndicatorTintColor = [UIColor blackColor];

        self.pageControl.currentPageIndicatorTintColor = [UIColor greenColor];


        self.pageControl.currentPage = i;

        UIImageView *image = [[UIImageView alloc] initWithFrame:
                              CGRectMake(xOrigin, 0,
                                         self.view.frame.size.width,
                                         self.view.frame.size.height)];

       image.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[imgArray objectAtIndex:i]]];      // imgArray is Array of Images

        image.contentMode = UIViewContentModeScaleToFill;

        [self.scrollView addSubview:image];
 }
    //set the scroll view content size
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width *
                                             numberOfViews,
                                             self.view.frame.size.height);

    //add the scrollview to this view
    [self.view addSubview:self.scrollView];


}

Thanks & Regards
Angel AppTech

Send Email from iOS

To Send Email from iOS Application you must have to import MessageUI Framework to your application.

To add this Framework Select -> Target -> Build Phases -> Link Binary with Libraries
Now import Framework in your file.


Next, click "+" button and select "MessageUI Framework" and click the Add button. After you click the Add button Framework will be added to your application. 



#import <MessageUI/MessageUI.h>

Add MFMailComposeDelegate 

@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate

Now write below code to send mail from our app. here I have added one button named "Send Email"and connected my below method to it to send the mail. You can put this code from where ever you wants to send mail.

- (IBAction)sendEmail:(id)sender
{
    // Email Subject
    NSString *emailTitle = @"Test mail";
    // Email Content
    NSString *messageBody = @"It is Mail for testing!";
    // To address
    NSArray *toRecipents = [NSArray arrayWithObject:@"ios@gmail.com"];
    
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc setToRecipients:toRecipents];
    
    // Present mail view controller on screen
    [self presentViewController:mc animated:YES completion:NULL];
    
}

This is Delegate method of MFMailComposeViewController. This method will be called automatically when the mail interface is closed. (e.g. user cancel the operation or mail sent)

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }
    
    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];

}


Build and Run and you will see just like that


Thanks & Regards
Angel AppTech



Set Textview Text in Center Vertically and Horizontally


To make the textview text horizontally center, select the textview from .xib class and go to the library and in that set Alignment as center.

But it is not easy to set is in centre for both vertically and Horizontally. 
but we can do it by observing the contentsize of UITextView, when there is any change in the contentSize, update the contentOffset.

Add observer as follows in viewDidLoad or viewWillAppear

[textview addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];

Handle the observer action as follows:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{
     UITextView *txtview = object;
     CGFloat topoffset = ([txtview bounds].size.height - [txtview contentSize].height * [txtview zoomScale])/2.0;
     topoffset = ( topoffset < 0.0 ? 0.0 : topoffset );
     txtview.contentOffset = (CGPoint){.x = 0, .y = -topoffset};
}

Remove observer in viewDidUnLoad or viewWillDisappear depends on where you add

[textview removeObserver:self forKeyPath:@"contentSize" context:NULL];


Thanks & Regards
Angel AppTech