alexbrie . com

A few recipes about dates / using NSDate

The app I’ve been working lately makes a bit more use of NSDate than my usual ones. I thought I’d share with the world a few pieces of code I’ve been using;

How to test if two NSDate dates belong to the same day:

I created a category on NSDate and added the following method to it:

-(BOOL)isSameDay:(NSDate *) otherDate
{
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents *comps_self = [[NSCalendar currentCalendar] components:unitFlags fromDate:self];
    NSDateComponents *comps_other = [[NSCalendar currentCalendar] components:unitFlags fromDate:otherDate];

    if ([comps_self day]==[comps_other day] &&
        [comps_self month]==[comps_other month] &&
        [comps_self year]==[comps_other year]) {
        return YES;
    }
    return NO;
}

I can now call [mydate isSameDay:otherdate]

Find out the first and last days in the month that contains a given date

-(void) updateMonthIntervalForDate:(NSDate*)date{
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents* dc = [[NSCalendar currentCalendar] components:unitFlags fromDate:date];
    dc.day=1;
    NSDate* startOfMonth = [[NSCalendar currentCalendar] dateFromComponents:dc];
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    offsetComponents.month=1;
    NSDate* endOfMonth = [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents toDate:self.startVisibleDate options:0];
    // ... do whatever you want with startOfMonth and endOfMonth
}

Last but not the least, print out nicely Print out the full name of the month, or the abridged name

First of all, create a NSDateFormatter; the docs says it’s not inexpensive, so if you need it several times, try and reuse it instead of recreating it over and over again.

    dateFormatter = [[NSDateFormatter alloc] init];

Next, decide the date format you want to output

Abridged version (like 01 Jan)
    [dateFormatter setDateFormat:@"dd MMM"];
Full month name (January)
    [dateFormatter setDateFormat:@"MMMM"];

Sure, this is in the docs as well, but I did quite a bit of searches to find out the part about MMMM for the full NSDate month name. So maybe it will help someone.

Once you have the date formatter ready, you can invoke it to get the nice string from your NSDate:

     NSString* mystring = [[dateFormatter stringFromDate:crtEntry.date] uppercaseString];

This was it. I wish you happy coding.

Adjusting UITextView size on Keyboard show/hide events, for iOS5

The time has come to, once again, blog over here as part of #idevblogaday.

Remember the project I was telling you last time? I can reveal it now, as it’s been on the AppStore for a while now – Clean Writer for iPad, 2.0, a fully recoded, feature rich redesigned version of my quite popular iPad plain-text editor for writers.

One thing that plagued the previous version was how the keyboard would cover up the text entry textview. I had tried to fix it by automatically adjusting the text view size, but there were always problems.

I ended up with a fix based on a recipe from Pragmatic Programmer’s iPad Programming book.

Read the rest of this entry »

UITextView, Apple quirks and cutting one’s losses

I’m in the middle of a project which excites me a lot, especially because I want to have it finished in the next couple of days, so that with a bit of luck the app would go live to the appstore before December 22 (the dreaded date when iTunesConnect will shut down for an entire week). I’ve only had 3.5 hours of sleep last night, and about 5 the night before.

One of the things I was doing at 5AM in the morning was trying to find the current point location of the cursor inside a UITextView (iOS 4.3+ or even 5.0 – whichever works).

I couldn’t.

Now, it’s not unusual to not be able to do things in iOS. What is unusual is that Apple’s docs said I should have been able to.

Let me explain. See, UITextView, according to the documentation, conforms to UITextInput protocol. UITextInput protocol, according to the documentation, has a required property selectedTextRange that gives “the range of the selected text in a document” and, in particular, the caret(insertion-point). It should be straightforward.  (PS. do not mistake selectedTextRange for selectedRange which returns just the range of the selection inside the text string.

To my frustration and despair, it didn’t work. My code would crash again and again complaining that selectedTextRange is an unrecognized selector. Even more frustrating, Google doesn’t offer any clue (except for one stackoverflow question and the fact that people enjoy giving stupid answers although they don’t understand the question). Or that others don’t think it’s possible.

So I gave up implementing what I wanted, cut my losses and moved on to implementing some other features. But if you have any idea about a decent way to achieve this (and, no, I don’t really want to use a custom 3rd party uitextview clone just to be able to know the CGPoint position of the cursor), I’d greatly appreciate your input.

Until then, ain’t it weird that a required property in a required protocol doesn’t seem to be  fully implemented in Apple’s one of the most important core UI objects?

Compacted Google Reader

After Google Reader’s UI change of today I, like millions of users, was furious. I won’t go into detail about the reasons (you can read them on my Romanian blog), but summing them up is easy: the new UI was probably designed on a couple of 30″ computer screens and is a visual insult to anyone trying to use it on a normal, smaller, screen. Regardless of the colors and fonts used, the new Google Reader UI is unusable due to its excessive use of whitespace which fills up the screen with unneeded padding and margins, while the actual content is crammed into a tiny space forcing you to scroll like a mad man.
Solutions started to appear pretty soon after, in the form of user-side scripts using browser extensions such as Greasemonkey or UserCSS.
Being a Safari user myself I followed a Twitter friend’s suggestion and created my own little UserCSS stylesheet which fixes the excessive whitespace issue, while keeping the new style.
If you are curious to try it, check out this page on Github. You can easily insert the style into a Greasemonkey script, if you wish.
From this..

..To this

Codesign issue – object file format invalid or unsuitable

Stack Overflow should receive the Nobel peace prize for removing 90% of developer’s wasted days, frustration and nerves So much frustration.
Thank you, Joel and Jeff for creating StackOverflow.
Like today, when it helped me with a bizzarre issue where codesigning my OSX app was failing on XCode 4.1..
http://stackoverflow.com/questions/4842717/iphone-codesign-object-file-format-invalid-or-unsuitable

PS. Or this one

Hi! I'm Alex Brie - developer, blogger, digerati, micro entrepreneur. I created tens of iOS & Mac apps including: Self Help Classics, Clean Writer minimalist editor(for Mac&iPad), the TouchBooksReader ebook framework and many more. I run my own company, , a tiny passionate app studio.

If you prefer email subscription:

Enter your email address:

Delivered by FeedBurner