Wednesday, June 24, 2015

Increase UILabel height dynamically:

CGRect frame = self.securityValueLbl.frame;
CGSize maximumLabelSize = CGSizeMake(frame.size.width, 80); //MAXFLOAT

NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin;

NSDictionary *attr = @{NSFontAttributeName: self.securityValueLbl.font};
CGRect expectedLabelFrame = [self.securityValueLbl.text boundingRectWithSize:maximumLabelSize
                                             options:options
                                          attributes:attr
                                             context:nil];

frame.size.height = ceilf(expectedLabelFrame.size.height);
self.securityValueLbl.frame = frame;


Tuesday, June 16, 2015

NSDate Helper Functions:

Get Today's Date:
===========================================

NSDate* date = [NSDate date];



Create a Date From Scratch:
===========================================

NSDateComponents* comps = [[NSDateComponents alloc]init];
comps.year = 2014;
comps.month = 3;
comps.day = 31;

NSCalendar* calendar = [NSCalendar currentCalendar];

NSDate* date = [calendar dateFromComponents:comps];



Add a day to a Date:
===========================================

NSDate* date = [NSDate date];

NSDateComponents* comps = [[NSDateComponents alloc]init];
comps.day = 1;

NSCalendar* calendar = [NSCalendar currentCalendar];

NSDate* tomorrow = [calendar dateByAddingComponents:comps toDate:date options:nil];



Subtract a day from a Date:
===========================================

NSDate* date = [NSDate date];

NSDateComponents* comps = [[NSDateComponents alloc]init];
comps.day = -1;

NSCalendar* calendar = [NSCalendar currentCalendar];

NSDate* yesterday = [calendar dateByAddingComponents:comps toDate:date options:nil];



Convert a Date to a String:
===========================================

NSDate* date = [NSDate date];

NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"MMMM dd, yyyy";
NSString* dateString = [formatter stringFromDate:date];



Convert a String to a Date:
===========================================

NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"MMMM dd, yyyy";
NSDate* date = [formatter dateFromString:@"August 02, 2014"];



Find how many days are in a month:
===========================================

NSDate* date = [NSDate date];

NSCalendar* cal = [NSCalendar currentCalendar];
NSRange currentRange = [cal rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:date];
NSInteger numberOfDays = currentRange.length;



Calculate how much time something took:
===========================================
NSDate* start = [NSDate date];

for(int i = 0; i < 1000000000; i++);

NSDate* end = [NSDate date];
NSTimeInterval duration = [end timeIntervalSinceDate:start];



Find the Day Of Week for a specific Date:
===========================================
NSDate* date = [NSDate date];
NSCalendar* cal = [NSCalendar currentCalendar];
NSInteger dow = [cal ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:date];



Enjoy!


Tuesday, June 9, 2015

Custom NSLog:


#define DSLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);



Get the last day of a month:

NSDate *curDate = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; // Get necessary date components

// set last of month
[comps setMonth:[comps month]+1];
[comps setDay:0];
NSDate *tDateMonth = [calendar dateFromComponents:comps];
NSLog(@"%@", tDateMonth);

Wednesday, June 3, 2015

NSFileProtectionKey :


// Create NSProtectionComplete attribute

NSDictionary *protectionComplete = [NSDictionary
dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];


// Set attribute on file at

[[[NSFileManager] defaultManager] setAttributes:protectionComplete
ofItemAtPath:filePath error:nil];


Tuesday, June 2, 2015

Dynamic height of UILabel


CGRect textRect = [text boundingRectWithSize:size
                                 options:NSStringDrawingUsesLineFragmentOrigin
                              attributes:@{NSFontAttributeName:FONT}
                                 context:nil];

CGSize size = textRect.size;