Posts

Showing posts from 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];       }          tableCe

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 add

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,                                          

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 *)t

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

Image
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://objective

UIPanGestureRecognizer to create Screen similar to Notification Center

Image
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 addGestureRecognize

Placeholder text in UITextView iOS

Image
As we all know UITextView does not have a Placeholder unlike a UITextField. So it is really a painful job to have a placeholder text in a UITextView. I got stuck on the same issue and did some work around. This trick will make you think that you are actually having a placeholder text inside a UITextView. So here we go: 1. Place a UILabel on nib (xib) file at the position where you want UITextView. 2. Now set text what you want on the placeholder and change it text color to lightGraycolor. 3. Take a UITextView and place it above the UILabel and set its background color to clearcolor. 4. It will appear as a placeholder inside UITextView. 5. Set delegates of UITextView as follows: In Header file: #import <UIKit/UIKit.h> @interface LoginViewController : UIViewController < UITextViewDelegate > @end In Implementation file: - ( void )viewDidLoad {     [ super viewDidLoad ];     [myTextView setDelegate:self]; } 6. Take textViewDidChange delega

Unique Identifier UUID - iOS

UDID is a hash value composed from various hardware identifiers such as the device serial number. It is unique for each device.  The UDID is independent of the device name, SIM card. It is really easy to get it from the device programmatically: NSString *uuid =  [[UIDevice currentDevice] uniqueIdentifier]; But UDID is no longer available onwards iOS 6 due to security / privacy reasons. UDID is deprecated by Apple; developers can not make use of it in any iOS application. So what now?  How can we identify the device? I found many solutions on searching over internet like  identifierForVendor or  advertisingIdentifier or generate our own UUID (store & persist it on your own). Many examples show how to generate UUID and store it in NSUserDefault: -( NSString *)generateUUID {     NSString *CFUUID = nil ;     if ([[ NSUserDefaults standardUserDefaults ] valueForKey : @"UUID" ] == nil ) {         CFUUIDRef uuid = CFUUIDCreate ( kCFAllocatorDef

Upload Multiple Images/Photos to Server Using MulipartFormatData via AFNetworking

Below is the code snippet which would help in uploading multiple Images along with Text to Server: -(void)uploadMultipleImagesWithTextMessageUsingAFNetworkingMultipartFormat:(id)sender {     // Upload multiple images to server using multipartformatdata (AFNetworking)         NSString *stringMessage = @"I am uploading multiple images to server";           AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL: [NSURL URLWithString:BASEURL]]; // replace BASEURL     client.parameterEncoding = AFJSONParameterEncoding;     NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/PostMultImagesWithTextAPI" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {                 [formData appendPartWithFormData:[[[NSUserDefaults standardUserDefaults] objectForKey:KServerAccessToken] dataUsingEncoding:NSUTF8StringEncoding] name:@"AccessToken"];                 [formData appen