Friday, 14 November 2014

iOS 6 Grouped Style UITableView in iOS 8 App

I came across a requirement where i have to show  iOS 6 grouped style UITableView in iOS8 App.
So i did some workaround and it really works.

Steps:

  • I added a UITableView on UIViewController (Scene) in UIStoryboard.
  • I reduced the width of the UITableView and centre aligned it (eg. UIViewController width - 320px so i take 300px UITableView).
  • Add & Import QuartzCore framework.
  • In cellForRowAtIndexPath method make the cell layer corner round as per requirement and set masktobounds as shown below.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"TableCell";
    
    UITableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (tableCell == nil) {
        tableCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      }
    
    tableCell.layer.cornerRadius = 5;
    tableCell.layer.masksToBounds = YES;
    
    [tableCell setBackgroundColor:[UIColor lightGrayColor]];
}


Wednesday, 23 April 2014

Text to Speech in iOS7

iOS7 makes Text to Speech really easy and that also in multiple languages.
iOS7 has added two new classes for Text to Speech functionality:
1. AVSpeechSynthesizer
2. AVSpeechUtterance

First Link AVFoundation and AudioToolbox Framework from Application Target -> Build Phases -> Link Binary With Libraries.

Import both frameworks in Header file (.h file)
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

add delegate <AVSpeechSynthesizerDelegate>.

In implementation file (.m file)
    AVSpeechSynthesizer *speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
    AVSpeechUtterance *speechUtterance = [AVSpeechUtterance speechUtteranceWithString:@"Welcome to iOS App."];
    speechUtterance.rate = AVSpeechUtteranceMaximumSpeechRate / 4.0f;
    speechUtterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"]; // language
    [speechSynthesizer speakUtterance: speechUtterance];

By adding the above code you can add text to speech functionality to your iOS7 app.

Display HTML in NSAttributedString in iOS7

iOS7 has really added some best things like displaying HTML in NSAttributedString.
Suppose in a case you want to display some HTML in your UITextView from any website.
Just pass the HTML to NSAttributedString and set this attributed string to the UITextView and that's it, you are done.

In below snippet i have passed the html string from my blog site to NSAttributedString and this NSAttributedString is set to UITextView.
Now the html from my blog site will be displayed in the UITextView along with clickable links.

      NSString *strHtml = [NSString stringWithContentsOfURL:[NSURL   URLWithString:@"http://objectivecwithsuraj.blogspot.in/"] encoding:NSUTF8StringEncoding error:nil];
    
    NSAttributedString *attrStrWithHtml = [[NSAttributedString alloc] 
                   initWithData:[strHtml dataUsingEncoding:NSUTF8StringEncoding]
                   options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                               NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                   documentAttributes:nil error:nil];
    
    UITextView *textView = [[UITextView alloc] initWithFrame:self.view.frame];
    [textView setAttributedText: attrStrWithHtml];
    [textView setDataDetectorTypes:UIDataDetectorTypeLink];
    [textView setEditable:NO];
    [self.view addSubview:textView];