Sharing a single NSDateFormatter instance

Formatting dates in cellForRowAtIndexPath or another method that gets called very often will make an app noticeably slower if an NSDateFormatter needs to be initialized on every call. Storing the NSDateFormatter in a property or making it static are both fine options but if it’s around anyway it might as well be used by the rest of the app. NSDateFormatter Instance Category //NSDateFormatter Category + (NSDateFormatter *)instance { static dispatch_once_t onceMark; static NSDateFormatter *formatter = nil; dispatch_once(&onceMark, ^{ formatter = [[NSDateFormatter alloc] init]; }); return formatter; } //Usage NSDateFormatter *dateformatter = [NSDateFormatter instance]; Drawbacks If there are multiple date formats in use the date format needs to be set before each use. Not a major drawback but it can get annoying. ...

August 15, 2013 · 1 min · Paul

Retina alterative to CGRectIntegral

CGRectIntegral is a function for rounding all the values of a CGRect at once, typically used for snapping frames to pixels. For a good example of how this helps prevent image blurring and other related rendering issues see this stackoverflow answer. On retina screens CGRectIntegral still rounds to the nearest point (1.0) rather than the nearest pixel (0.5) which isn’t ideal. My solution to this is implemented as a UIView category which matches my use case for CGRectIntegral 100% of the time. Your mileage may vary. ...

August 14, 2013 · 1 min · Paul

Mac constantly switching to a new finder window when the space key is pressed

If whenever you press the space key your mac shifts focus to a finder window you should be able to fix it by hard booting Finder. Open terminal and run: killall Finder

August 13, 2013 · 1 min · Paul

Unarchiving 7z files on OS X

Brew is always saving the day. # Install p7zip brew install p7zip # extract 7z x file.7z

August 12, 2013 · 1 min · Paul

Quick Browser Detection One Liners

// You can expand this out in any line below // but it makes things much more readable var ua = navigator.userAgent.toString().toLowerCase(); //Browsers var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/ var IE7 = (document.all && !window.opera && window.XMLHttpRequest && ua.indexOf('trident/4.0') == -1) ? true : false; var IE8 = (ua.indexOf('trident/4.0') != -1); var IE9 = ua.indexOf("trident/5")>-1; var IE10 = ua.indexOf("trident/6")>-1; var SAFARI = (ua.indexOf("safari") != -1) && (ua.indexOf("chrome") == -1); var FIREFOX = (ua.indexOf("firefox") != -1); var CHROME = (ua.indexOf("chrome") != -1); //Platforms var MAC = (ua.indexOf("mac")!=-1) ? true: false; var WINDOWS = (navigator.appVersion.indexOf("Win")!=-1) ? true : false; var LINUX = (navigator.appVersion.indexOf("Linux")!=-1) ? true : false; var UNIX = (navigator.appVersion.indexOf("X11")!=-1) ? true : false; var IOS = ((ua.indexOf("iphone")!=-1) || (ua.indexOf("ipod")!=-1) || (ua.indexOf("ipad")!=-1)) ? true : false; var ANDROID = ua.indexOf("android")!=-1) ? true: false; var BLACKBERRY = (ua.indexOf("blackberry")!=-1) ? true: false; //mobile browsers var OPERA_MINI = (ua.indexOf("opera mini")!=-1) ? true: false;

August 9, 2013 · 1 min · Paul

Vertical space in bootstrap

I use Bootstrap for most personal projects lately but one thing that consistently gets me is the lack of vertical spacing classes. Quick fix I added these classes to my bootstrap stylesheet .voffset { margin-top: 2px; } .voffset1 { margin-top: 5px; } .voffset2 { margin-top: 10px; } .voffset3 { margin-top: 15px; } .voffset4 { margin-top: 30px; } .voffset5 { margin-top: 40px; } .voffset6 { margin-top: 60px; } .voffset7 { margin-top: 80px; } .voffset8 { margin-top: 100px; } .voffset9 { margin-top: 150px; } Example Vertically offset text

August 8, 2013 · 1 min · Paul

Checking if a UITableViewCell is fully visible

Sometime you need to know if a UITableViewCell is completely visible and for those times there’s this handy UITableViewCell category. //Place in UITableViewCell Category - (BOOL) isCompletelyVisible { // For parents category see pdenya.com/g/uiview_parents UITableView *tableview = (UITableView *)[self parents:[UITableView class]]; CGRect rect = [tableview rectForRowAtIndexPath:[tableview indexPathForCell:self]]; rect = [tableview convertRect:rect toView:tableview.superview]; BOOL completelyVisible = CGRectContainsRect(tableview.frame, rect); return completelyVisible; } I’d like to mention that you can skip this check entirely if your goal is to make sure a cell is completely visible. With UITableViewScrollPositionNone scrollToRowAtIndexPath:atScrollPosition:animated: doesn’t scroll at all if the cell is totally visible and in other cases it scrolls as little as possible which is usually the ideal behavior. ...

August 7, 2013 · 1 min · Paul

HelloSign for Gmail

HelloSign for Gmail is a browser extension for Chrome, FireFox and Safari that lets users fill out and sign documents without ever leaving Gmail. The user clicks the sign button next to an attachment and the file is uploaded to HelloSign and the HelloSign editor is opened in a lightbox for the user to sign and make annotations. When the user clicks Continue the iframe closes, a compose window opens in Gmail (as if the user had clicked reply) and the signed document is attached, ready to be sent. ...

August 6, 2013 · 2 min · Paul

Tap anywhere else to cancel in Objective C

Whenever you have a modal element that outside touches should cancel a few things need to happen: Touches inside the modal element should behave normally Outside touches should not trigger touch events on tappable elements Outside touches should trigger a handler to close the modal In my case I was swiping a UITableViewCell and revealing a controls view that should close if any other UITableViewCell is tapped. hitTest:withEvent - The easiest method Touch events bubble down from the root view rather than up from the target view which makes this a bit easier. On whichever view needs to capture the touch events, we need to override hitTest:withEvent: to return nil and call a function whenever a modal is open and a view other than the modal is tapped. I overrode this method in my UITableView’s superview. ...

August 6, 2013 · 3 min · Paul

Hiding the WP Admin Bar

There’s a few plugins for hiding the WordPress admin bar that shows up on top of your site when you’re logged in but it seems like overkill. If you don’t care about preventing it from loading you can drop this in your stylesheet at wp-admin/themes.php?page=editcss to hide it. html { margin-top: 0!important; } #wpadminbar { display: none; }

August 3, 2013 · 1 min · Paul