UIImage+Extension
#import <UIKit/UIKit.h> @interface UIImage (Extension) /* * * 不同版本系统适配使用 */ + (UIImage *)imageWithName:(NSString *)name; /* * * 通过颜色来创建一张图片 */ + (UIImage *)imageWithColor:(UIColor *)color; /* * * 将图片切成圆形 */ +(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset;
@end
#import " UIImage+Extension.h " @implementation UIImage (Extension) /* * * 根据图片名,自动适配系统 */ + (UIImage *)imageWithName:(NSString *)name { UIImage *image = nil; if (iOS8) { NSString *newName = [name stringByAppendingString: @" _os8 "]; image = [UIImage imageNamed:newName]; } if (image == nil) { image = [UIImage imageNamed:name]; } return image; } /* * * 通过颜色来创建一张图片 */ + (UIImage *)imageWithColor:(UIColor *)color { CGFloat imageW = 100; CGFloat imageH = 100; // 1.开启基于位图的图形上下文 UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageW, imageH), NO, 0.0); // 2.画一个color颜色的矩形框 [color set]; UIRectFill(CGRectMake( 0, 0, imageW, imageH)); // 3.拿到图片 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); // 4.关闭上下文 UIGraphicsEndImageContext(); return image; } /* * * 将图片切成圆形 */ +(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset { UIGraphicsBeginImageContext(image.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 2); CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.0f, image.size.height - inset * 2.0f); CGContextAddEllipseInRect(context, rect); CGContextClip(context); [image drawInRect:rect]; CGContextAddEllipseInRect(context, rect); CGContextStrokePath(context); UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newimg; }
@end
UIView
#import <UIKit/UIKit.h>
@interface UIView (Extension) @property (nonatomic, assign) CGFloat x; @property (nonatomic, assign) CGFloat y; @property (nonatomic, assign) CGFloat centerX; @property (nonatomic, assign) CGFloat centerY; @property (nonatomic, assign) CGFloat width; @property (nonatomic, assign) CGFloat height; @property (nonatomic, assign) CGSize size; @end
#import " UIView+Extension.h " @implementation UIView (Extension) - (CGFloat)x { return self.frame.origin.x; } - ( void)setX:(CGFloat)x { CGRect frame = self.frame; frame.origin.x = x; self.frame = frame; } - (CGFloat)y { return self.frame.origin.y; } -( void)setY:(CGFloat)y { CGRect frame = self.frame; frame.origin.y = y; self.frame = frame; } - (CGFloat)centerX { return self.center.x; } -( void)setCenterX:(CGFloat)centerX { CGPoint center = self.center; center.x = centerX; self.center = center; } - (CGFloat)centerY { return self.center.y; } - ( void)setCenterY:(CGFloat)centerY { CGPoint center = self.center; center.y = centerY; self.center = center; } - (CGFloat)width { return self.frame.size.width; } - ( void)setWidth:(CGFloat)width { CGRect frame = self.frame; frame.size.width = width; self.frame = frame; } -(CGFloat)height { return self.frame.size.height; } - ( void)setHeight:(CGFloat)height { CGRect frame = self.frame; frame.size.height = height; self.frame = frame; } - (CGSize)size { return self.frame.size; } - ( void)setSize:(CGSize)size { CGRect frame = self.frame; frame.size = size; self.frame = frame; }
@end
NSDate
#import <Foundation/Foundation.h>
@interface NSDate (MJ) /* * * 是否为今天 */ - (BOOL)isToday; /* * * 是否为昨天 */ - (BOOL)isYesterday; /* * * 是否为今年 */ - (BOOL)isThisYear; /* * * 返回一个只有年月日的时间 */ - (NSDate *)dateWithYMD; /* * * 获得与当前时间的差距 */ - (NSDateComponents *)deltaWithNow; @end
#import " NSDate+MJ.h " @implementation NSDate (MJ) /* * * 是否为今天 */ - (BOOL)isToday { NSCalendar *calendar = [NSCalendar currentCalendar]; int unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear; // 1.获得当前时间的年月日 NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]]; // 2.获得self的年月日 NSDateComponents *selfCmps = [calendar components:unit fromDate:self]; return (selfCmps.year == nowCmps.year) && (selfCmps.month == nowCmps.month) && (selfCmps.day == nowCmps.day); } /* * * 是否为昨天 */ - (BOOL)isYesterday { // 2014-05-01 NSDate *nowDate = [[NSDate date] dateWithYMD]; // 2014-04-30 NSDate *selfDate = [self dateWithYMD]; // 获得nowDate和selfDate的差距 NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options: 0]; return cmps.day == 1; } - (NSDate *)dateWithYMD { NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @" yyyy-MM-dd "; NSString *selfStr = [fmt stringFromDate:self]; return [fmt dateFromString:selfStr]; } /* * * 是否为今年 */ - (BOOL)isThisYear { NSCalendar *calendar = [NSCalendar currentCalendar]; int unit = NSCalendarUnitYear; // 1.获得当前时间的年月日 NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]]; // 2.获得self的年月日 NSDateComponents *selfCmps = [calendar components:unit fromDate:self]; return nowCmps.year == selfCmps.year; } - (NSDateComponents *)deltaWithNow { NSCalendar *calendar = [NSCalendar currentCalendar]; int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; return [calendar components:unit fromDate:self toDate:[NSDate date] options: 0]; }
@end
UIBarButtonItem