Category Archives: Programming

UITextView Padding

If you align a UITextView with another element, particularly a UILabel, using Auto Layout you will notice the text in the UITextView is slightly indented. The UITextView is actually implemented inside an NSTextContainer which has some padding around it. To remove this (left/right) padding, set the following property of the UITextView to zero:

textView.textContainer.lineFragmentPadding = 0

 

Encoding a URL in iOS 9

The typical way to encode URLs (e.g. turn spaces into %20) was to use NSString’s

stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding

As you may have noticed, that method has been deprecated in iOS 9. You now need to use

stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet

which takes an NSCharacterSet as it’s argument. What NSCharacterSet am I supposed to use you may ask. I recently ran into this and found the following worked for me:

[NSCharacterSet URLFragmentAllowedCharacterSet]

The relevant Apple documentation can be found here.

Ctrl-drag to Create Outlets in Custom Views

If anyone can tell me why Xcode (I’m using Xcode 7 beta 5) works this way, I’d greatly appreciate. I have created custom table view cells in the past and I’ve always control-dragged from my storyboard into the custom class to create the outlets. So, when I went to create outlets for a custom (ui)view I’m working on, I was stumped when I couldn’t ctrl-drag into the .h or .m file to create them (outlets or actions).

[Yes, I have all the relevant custom classes specified in the Identity inspector.]

The workaround is to type in the outlet/action code by hand. Once you have entered in your custom class file, you can connect the outlets/actions by dragging from the little dot (do these things have a special name?) Xcode puts in the line number gutter to the relevant UI element. What a pain in the ass.

</endOfXcodeRantForToday>

WordPress Menu Button on Mobile

A quick note regarding WordPress and its Menu button on mobile devices (mobile Safari for sure, others possibly). I’ve been trying to update this site a little and added a Books page to highlight the book I’m currently reading, along with books I’ve read over the last year. I decided to track the books using Pinterest and figured out how to post a Pin and a Board on a page. I decided I should check my handy work on my iPhone and quickly realized the “Menu” button didn’t work. Hmm…

As it turns out, WordPress updated the CSS tag used for the Menu button in their Twenty Twelve theme’s header.php file. So, if you have a child theme like I do and didn’t know about this update, your Menu button is no longer working.

Fortunately, it’s a very quick fix. Open the header.php in your child theme folder and replace the following line of code:

 <h3 class="menu-toggle"><?php _e( 'Menu', 'twentytwelve' ); ?></h3>

with:

 <button class="menu-toggle"><?php _e( 'Menu', 'twentytwelve' ); ?></button>

The navigation.js file was (also) updated to look for this new tag, which is why <h3> doesn’t work anymore. That’s it, all should be working again.

Gesture Recognizers in iOS 9

I don’t code nearly as much as I’d like to, so keeping up-to-date is always a major challenge. I’m still trying to get up to speed on all the great iOS 8 updates with iOS 9 is right around the corner. So, this is probably not news to you but it was news to me.

Prior to iOS 9, you could have a gesture recognizer (GR) associated with 2 different views. You weren’t supposed to but you could. After wiring the GR to both views, you could add the UIGestureRecognizerDelegate method:

– (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

return YES, and Bob was your uncle. If you do that in iOS 9, you get a very nice error message stating you were never supposed to do that, it’s now being enforced, and the GR will only be added to the view that first loads it. Having figured that out, okay, no biggie, I removed one of my views from all my GRs and then added “duplicate” GRs to the other view, named them differently so I knew which were which, and wired them to the same action method. All good to go.

P.S. Here’s the relevant Apple Documentation page. Scroll down to:

Gesture Recognizers Are Attached to a View

Every gesture recognizer is associated with one view. By contrast, a view can have multiple gesture recognizers, because a single view might respond to many different gestures. For a gesture recognizer to recognize touches that occur in a particular view, you must attach the gesture recognizer to that view. When a user touches that view, the gesture recognizer receives a message that a touch occurred before the view object does. As a result, the gesture recognizer can respond to touches on behalf of the view.

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.

Parsing Strava’s GPX File in PHP

I love Strava! However, as with all tracking sites, Strava doesn’t always provide the exact window I want into my data. I’ve been thinking about building an app that allows me to manipulate the specific data I’m interested in but to do that, one has to get the data first. Given that, I’ve been playing around with parsing Strava GPX files. I’ve been looking at Garmin GPX and FIT files as well, but the Strava ones seem the easiest to work with.

The challenge I had with the Strava GPX file was getting at the data in the extensions: temperature (atemp), heart rate (hr) and Cadence (cad) data. It took me a few hours to figure out how to parse those values (I’m not very good at XML, namespaces, etc.), so I wanted to post this bit of PHP code to hopefully save others some time!

foreach($stravaData->trk->trkseg->children() as $trkpts) {
    $dataPoint->trkpt = $i;
    $dataPoint->latitude = $trkpts['lat'];
    $dataPoint->longitude = $trkpts['lon'];
    $dataPoint->elevation = $trkpts->ele;
    $dataPoint->time = $trkpts->time;
    $dataPoint->temp = $trkpts->extensions->children('gpxtpx', true)->TrackPointExtension->atemp;
    $dataPoint->hr = $trkpts->extensions->children('gpxtpx', true)->TrackPointExtension->hr;
    $dataPoint->cad = $trkpts->extensions->children('gpxtpx', true)->TrackPointExtensi
 on->cad;
    ...
}

The above code assumes you have created a “dataPoint” class to store the relevant Strava data (trackpoint number, latitude, longitude, elevation, time, temp, hr, cad, … you may have others like power). Let me know if you have any questions.