Filed under: Objective-C

UILabel Vertical / Bottom Align

I find it hard to believe that Apple when developing the iOS SDK, did not take into account that users may wish to align label text, either to the top or the bottom of a label. Instead they simply give the option of aligning it to the center, as shown below:

20090628-jkf727u2wsbsrgfhep3ja

This code allows you to align text either to the top or the bottom of a UILabel

I hope this helps someone else struggling with this issue, maybe it will be fixed in iOS 5.

Shameless Plug

(download)

I just wanted to take a second to let everyone know about Countdown, an application developed by myself which is available in the Apple App Store.

The purpose of countdown is simple, you enter a date and it shows you a countdown to that date and notifies you when the date is imminent. It supports multiple dates, local notifications and advanced user interface customisation.

What's more it is totally free and available now! Grab your copy today!!!

http://itunes.apple.com/gb/app/countdown/id343020892?mt=8

NSDate - Zero Out Seconds

I recently had a scenario where I was getting the current date using:

Now in many scenarios, this code will work perfectly, but the issue I had was I was performing some quite advanced calculations on the date, and the date contained a value for seconds, which threw my calculations off a little, because I hadn't factored seconds into my equations.

The following code, when passed a NSDate object will 'zero' out the second value, leaving you with an absolute value;

Useful NSDate Functions

I often find that I have to create dates based upon other dates. NSDate doesn't allow for the easy creation of dates; for example, what if you wanted to create a date which was exactly 5 minutes from now? It would require quite complex code to be wrote each time you wanted to create the date.
So I wrote this simple class which takes the some pain away from date manipulation within Objective-C.

 
// 
// DateFunctions.h 
// 
// Created by Mick Walker on 26/06/2010. 
// Copyright 2010 Mick Walker. All rights reserved. 
// 
#import <Foundation/Foundation.h> 
@interface DateFunctions : NSObject { 
} 
+ (NSDate *) dateWithDaysFromNow: (NSUInteger) days; 
+ (NSDate *) dateWithDaysBeforeNow: (NSUInteger) days; 
+ (NSDate *) dateWithHoursFromNow: (NSUInteger) dHours; 
+ (NSDate *) dateWithHoursBeforeNow: (NSUInteger) dHours; 
+ (NSDate *) dateWithMinutesFromNow: (NSUInteger) dMinutes; 
+ (NSDate *) dateWithMinutesBeforeNow: (NSUInteger) dMinutes; 
+ (NSDate *) dateTomorrow; 
+ (NSDate *) dateYesterday; 
+ (NSDate *) dateWithDaysFromDate:(NSDate *) date numberOfDays:(NSUInteger) days; 
+ (NSDate *) dateWithDaysBeforeDate:(NSDate *) date numberOfDays:(NSUInteger) days; 
+ (NSDate *) dateWithHoursFromDate:(NSDate *) date numberOfHours:(NSUInteger) hours; 
+ (NSDate *) dateWithHoursBeforeDate:(NSDate *) date numberOfHours:(NSUInteger) hours; 
+ (NSDate *) dateWithMinutesFromDate:(NSDate *) date numberOfMinutes:(NSUInteger) mins; 
+ (NSDate *) dateWithMinutesBeforeDate:(NSDate *) date numberOfMinutes:(NSUInteger) mins; 
+ (BOOL) isDateToday:(NSDate *) date; 
+ (NSInteger) numberOfDaysAway:(NSDate *) date; 
@end 
 
// 
// DateFunctions.m 
// 
// Created by Mick Walker on 26/06/2010. 
// Copyright 2010 Mick Walker. All rights reserved. 
// 
#import "DateFunctions.h" 
 
#define D_MINUTE 60 
#define D_HOUR 3600 
#define D_DAY 86400 
#define D_WEEK 604800 
#define D_YEAR 31556926 
 
#define DATE_COMPONENTS (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit) 
#define CURRENT_CALENDAR [NSCalendar currentCalendar] 
@implementation DateFunctions 
+ (NSDate *) dateWithDaysFromNow: (NSUInteger) days 
{ 
 NSTimeInterval aTimeInterval = [[NSDate date] 
 timeIntervalSinceReferenceDate] + D_DAY * days; 
 NSDate *newDate = [NSDate 
 dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; 
 return newDate; 
} 
 
+ (NSDate *) dateWithDaysBeforeNow: (NSUInteger) days 
{ 
 NSTimeInterval aTimeInterval = [[NSDate date] 
 timeIntervalSinceReferenceDate] - D_DAY * days; 
 NSDate *newDate = [NSDate 
 dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; 
 return newDate; 
} 
 
+ (NSDate *) dateWithHoursFromNow: (NSUInteger) dHours 
{ 
 NSTimeInterval aTimeInterval = [[NSDate date] 
 timeIntervalSinceReferenceDate] + D_HOUR * dHours; 
 NSDate *newDate = [NSDate 
 dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; 
 return newDate; 
} 
 
+ (NSDate *) dateWithHoursBeforeNow: (NSUInteger) dHours 
{ 
 NSTimeInterval aTimeInterval = [[NSDate date] 
 timeIntervalSinceReferenceDate] - D_HOUR * dHours; 
 NSDate *newDate = [NSDate 
 dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; 
 return newDate; 
} 
 
+ (NSDate *) dateWithMinutesFromNow: (NSUInteger) dMinutes 
{ 
 NSTimeInterval aTimeInterval = [[NSDate date] 
 timeIntervalSinceReferenceDate] + D_MINUTE * dMinutes; 
 NSDate *newDate = [NSDate 
 dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; 
 return newDate; } 
 
+ (NSDate *) dateWithMinutesBeforeNow: (NSUInteger) dMinutes 
{ 
 NSTimeInterval aTimeInterval = [[NSDate date] 
 timeIntervalSinceReferenceDate] - D_MINUTE * dMinutes; 
 NSDate *newDate = [NSDate 
 dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; 
 return newDate; } 
 
+ (NSDate *) dateTomorrow 
{ 
 return [self dateWithDaysFromNow:1]; 
} 
 
+ (NSDate *) dateYesterday 
{ 
 return [self dateWithDaysBeforeNow:1]; 
} 
 
+ (NSDate *) dateWithDaysFromDate:(NSDate *) date numberOfDays:(NSUInteger) days { 
 NSTimeInterval aTimeInterval = [date 
 timeIntervalSinceReferenceDate] + D_DAY * days; 
 NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; 
 return newDate; 
} 
 
+ (NSDate *) dateWithDaysBeforeDate:(NSDate *) date numberOfDays:(NSUInteger) days { 
 NSTimeInterval aTimeInterval = [date timeIntervalSinceReferenceDate] - D_DAY * days; 
 NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; 
 return newDate; 
} 
// 
+ (NSDate *) dateWithHoursFromDate:(NSDate *) date numberOfHours:(NSUInteger) hours { 
 
 NSTimeInterval aTimeInterval = [date timeIntervalSinceReferenceDate] + D_HOUR * hours; 
 NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; 
 return newDate; 
} 
 
+ (NSDate *) dateWithHoursBeforeDate:(NSDate *) date numberOfHours:(NSUInteger) hours { 
 NSTimeInterval aTimeInterval = [date timeIntervalSinceReferenceDate] - D_HOUR * hours; 
 NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; 
 return newDate; 
} 
 
+ (NSDate *) dateWithMinutesFromDate:(NSDate *) date numberOfMinutes:(NSUInteger) mins { 
 NSTimeInterval aTimeInterval = [date timeIntervalSinceReferenceDate] + D_MINUTE * mins; 
 NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; 
 return newDate; 
} 
+ (NSDate *) dateWithMinutesBeforeDate:(NSDate *) date numberOfMinutes:(NSUInteger) mins { 
 NSTimeInterval aTimeInterval = [date timeIntervalSinceReferenceDate] - D_MINUTE * mins; 
 NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; 
 return newDate; 
} 
 
+ (BOOL) isDateToday:(NSDate *) date { 
 NSDateComponents *otherDay = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date]; 
 NSDateComponents *today = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]]; 
 if([today day] == [otherDay day] && 
 [today month] == [otherDay month] && 
 [today year] == [otherDay year]) { 
 //do stuff 
 return YES; 
 } 
 return NO; 
 
} 
+ (NSInteger) numberOfDaysAway:(NSDate *) date { 
 return 1; 
} 
 
@end 

I hope this helps someone else.

Sorting a NSMutableArray Randomly - Shuffle

Earlier today, I had a situation where I needed the contents of a NSMutableArrray in a random order.

There are many way in which this could be achieved, however I decided to build a Category onto NSMutableArray itself to aid in the reuse of the code, and to provide some features out of the box because the project I was actually developing was a static library for use in other project.
I am posting the code here in case anyone else finds it useful, and so I know where it is, should I ever need it again in the future.
interface:
//  NSMutableArray_Shuffling.h
//  Created by Mick Walker on 11/04/2011.
//
 
#if TARGET_OS_IPHONE
#import "UIKit/UIKit.h"
#else
#include <Cocoa/Cocoa.h>
#endif
 
 
@interface NSMutableArray (Shuffling)
- (void) shuffle;
@end

implementation:
//  NSMutableArray_Shuffling.m
//  Created by Mick Walker on 11/04/2011.
//
 
#import "NSMutableArray_Shuffling.h"
 
 
@implementation NSMutableArray (Shuffling)
 
-(void) shuffle {
    NSUInteger count = [self count];
    for(NSUInteger i = 0; i < count; i++){
        int nElements = count -1;
        int n = (arc4random() %nElements) + 1;
        [self exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
}
@end

Using the code is simple, simply import the header file:
#import "NSMutableArray_Shuffling.h"

And then on an initialised NSMutableArray object call:
[someArray shuffle];
I hope that this post helps someone.