Tag Archives: iOS

Posts that include information about Apple’s mobile operating system or about programming for iOS.

Creating a Transparent Modal UIViewController in iOS 8

I’ve just started to scratch the surface of all the cool things iOS 8 can do, so much to learn! One of my apps presents a modal view controller that asks the user to select a date from a custom calendar view I built. The app displays photos and this modal vc is presented over an image, so I wanted the modal vc to be (semi) transparent, so the image could be seen behind it. I scoured the web but there seemed to be a lot of conflicting ideas out there and none of them worked for me. I happened to stumble upon a/the solution on my own, while poking around Attributes Inspector.

Select the (modal’s – i.e. the one you want to be transparent) view controller from your storyboard and then click on the Attributes Inspector. As you can see in the screen shot below, there are Transition Style and Presentation options now available. The Transition Style is just what you expect it is and for Presentation, you want to select Over Current Context. Viola! Crazy simple.

Attributes Inspector

I’d be interested to hear what has worked for you in iOS 7, although backward compatibility is quickly becoming less interesting to me.

How To Convert HTML to NSAttributedString

I’m not sure what the best practice is these days when displaying HTML content in your app. It seems you can either put it into a UIWebView or you can, as of iOS 7, convert it to an NSAttributedString and put it into a UILabel, UITextField, etc. I have been leaning toward the latter recently and thought I’d post the 2 lines of code required to make that happen.

NSAttributedString *stringWithHTMLAttributes = [[NSAttributedString alloc] initWithData:[self.htmlText dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)} documentAttributes:nil error:nil];

self.htmlLabel.attributedText = stringWithHTMLAttributes;

The above code assumes you have HTML stored in an NSString variable called “self.htmlText” and a UILabel called htmlLabel and that your encoding is UTF8.

Let me know if you have any questions or if there is a better practice I’m not aware of.

How to Convert an ISO8601 Date in iOS

I still find working with dates in iOS a little tricky. There is always a new format to deal with and it’s all about getting the identifiers for the format just right! I recently ran into ISO8601 and hope the bit of code below is of some use.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
NSDate *date = [dateFormatter dateFromString:self.releaseDate];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
self.dateString = [dateFormatter stringFromDate:date];

You can, of course, pick any other date format style (instead of Long) as the output. As always, let me know if you have any questions.