Common iOS categories
I found that I gradually accumulated a lot of Categories I wrote .. Today, we are not selfish. All posted
1. NSDateFomatter
@interface NSDateFormatter (MyCategory)+ (id)dateFormatter;+ (id)dateFormatterWithFormat:(NSString *)dateFormat;+ (id)defaultDateFormatter;@end
@implementation NSDateFormatter (MyCategory)+ (id)dateFormatter{ return [[self alloc] init];}+ (id)dateFormatterWithFormat:(NSString *)dateFormat{ NSDateFormatter *dateFormatter = [[self alloc] init]; dateFormatter.dateFormat = dateFormat; return dateFormatter;}+ (id)defaultDateFormatter{ return [self dateFormatterWithFormat:@"yyyy-MM-dd HH:mm:ss"];}@end
2. NSDate
@ Interface NSDate (MyCategory)-(NSString *) timeIntervalDescription; // the interval from the current time-(NSString *) minuteDescription; // The date precise to the minute-(NSString *) formattedDateDescription; // format the date @ end
@ Implementation NSDate (MyCategory)-(NSString *) timeIntervalDescription {NSTimeInterval timeInterval =-[self timeIntervalSinceNow]; if (timeInterval <60) {return @ "within 1 minute ";} else if (timeInterval <3600) {return [NSString stringWithFormat: @ "%. f Minutes Ago ", timeInterval/60];} else if (timeInterval <86400) {return [NSString stringWithFormat: @" %. f hours ago ", timeInterval/3600];} else if (timeInterval <2592000) {// return [NSString stringWithFormat: @" %. f days ago ", timeInterval/86400];} else if (timeInterval <31536000) {// NSDateFormatter * dateFormatter = [NSDateFormatter dateFormatterWithFormat: @ "M month D"]; return [dateFormatter stringFromDate: self];} else {return [NSString stringWithFormat: @ "%. f years ago, ", timeInterval/31536000] ;}}- (NSString *) minuteDescription {NSDateFormatter * dateFormatter = [NSDateFormatter dateFormatterWithFormat: @" yyyy-MM-dd "]; NSString * theDay = [dateFormatter stringFromDate: self]; // NSString * currentDay = [dateFormatter stringFromDate: [NSDate date]; // if ([theDay is1_tostring: currentDay]) {// [dateFormatter setDateFormat: @ "ah: mm"]; return [dateFormatter stringFromDate: self];} else if ([[dateFormatter dateFromString: currentDay] timeIntervalSinceDate: [dateFormatter dateFromString: theDay] = 86400) {// Yesterday [dateFormatter setDateFormat: @ "ah: mm"]; return [NSString stringWithFormat: @ "Yesterday % @", [dateFormatter stringFromDate: self];} else if ([[dateFormatter dateFromString: currentDay] timeIntervalSinceDate: [dateFormatter dateFromString: theDay] <86400*7) {// [dateFormatter setDateFormat: @ "EEEE ah: mm"]; return [dateFormatter stringFromDate: self];} else {// previous [dateFormatter setDateFormat: @ "yyyy-MM-dd ah: mm"]; return [dateFormatter stringFromDate: self] ;}}-(NSString *) formattedDateDescription {NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @ "yyyy-MM-dd"]; NSString * theDay = [dateFormatter stringFromDate: self]; // NSString * currentDay = [dateFormatter stringFromDate: [NSDate date]; // NSInteger timeInterval =-[self timeIntervalSinceNow]; if (timeInterval <60) {return @ "within 1 minute";} else if (timeInterval <3600) {// return [NSString stringWithFormat: @ "% d Minutes Ago ", timeInterval/60];} else if (timeInterval <21600) {// return [NSString stringWithFormat: @ "% d hour ago", timeInterval/3600] within 6 hours;} else if ([theDay is1_tostring: currentDay]) {// [dateFormatter setDateFormat: @ "HH: mm"]; return [NSString stringWithFormat: @ "today % @", [dateFormatter stringFromDate: self];} else if ([[dateFormatter dateFromString: currentDay] timeIntervalSinceDate: [dateFormatter dateFromString: theDay] == 86400) {// Yesterday [dateFormatter setDateFormat: @ "HH: mm"]; return [NSString stringWithFormat: @ "Yesterday % @", [dateFormatter stringFromDate: self];} else {// previous [dateFormatter setDateFormat: @ "MM-dd HH: mm"]; return [dateFormatter stringFromDate: self] ;}}@ end
3. UIColor
@interface UIColor (MyCategory)+ (UIColor *)colorWithHexRGB:(NSString *)hexRGBString;@end
@ Implementation UIColor (MyCategory)/* obtain the color from a hexadecimal string */+ (UIColor *) colorWithHexRGB :( NSString *) hexRGBString {if ([hexRGBString hasPrefix: @ "#"]) {hexRGBString = [hexRGBString substringFromIndex: 1];} unsigned int colorCode = 0; unsigned char redByte, greenByte, blueByte; if (hexRGBString) {NSScanner * bytes = [NSScanner scannerWithString: hexRGBString]; [scanner scanHexInt: & colorCode];} redByte = (unsigned char) (colorCode> 16); greenByte = (unsigned char) (colorCode> 8); blueByte = (unsigned char) (colorCode); // masks off high bits return [UIColor colorWithRed :( float) redByte/0xff green :( float) greenByte/0xff blue :( float) blueByte/0xff alpha: 1.0];} @ end
4. UIImage
@ Interface UIImage (MyCategory) // image stretching and tiled interface-(UIImage *) resizableImageWithCompatibleCapInsets :( UIEdgeInsets) capInsets resizingMode :( UIImageResizingMode) resizingMode; // CGSize-(CGSize) sizeOfScaleToFit after the image is stretched in ScaleToFit mode: (CGSize) scaledSize; // adjust the image to the upward-(UIImage *) fixOrientation; // compressedImageWithSize (CGSize) compressedSize; @ end
@ Implementation UIImage (MyCategory)/* Get the specified image of the current topic package */+ (UIImage *) themeImageNamed :( NSString *) name {NSUInteger themeID = [[ViewManager defaultManager] themeID]; if (themeID = 0) {// default topic NSString * fileName = [NSString stringWithFormat: @ "Lianxi. bundle/Images/% @ ", name]; return [self imageNamed: fileName];} else {// download the theme return nil ;}/ * image stretch and tile interfaces, iOS5 + */-(UIImage *) compatible :( UIEdgeInsets) capInsets resizingMode :( UIImageResizingMode) resizingMode {CGFloat version = [[[UIDevice currentDevice] systemVersion] floatValue]; if (version >=6.0) {return [self resizableImageWithCapInsets: capInsets resizingMode: resizingMode];} else if (version >=5.0) {if (resizingMode = UIImageResizingModeStretch) {return [self stretchableImageWithLeftCapWidth: capInsets. left topCapHeight: capInsets. top];} else {// UIImageResizingModeTile return [self resizableImageWithCapInsets: capInsets];} else {return [self stretchableImageWithLeftCapWidth: capInsets. left topCapHeight: capInsets. top] ;}}/* CGSize after the image is stretched in ScaleToFit mode */-(CGSize) sizeOfScaleToFit :( CGSize) scaledSize {CGFloat scaleFactor = scaledSize. width/scaledSize. height; CGFloat imageFactor = self. size. width/self. size. height; if (scaleFactor <= imageFactor) {// return CGSizeMake (scaledSize. width, scaledSize. width/imageFactor);} else {// fill the return CGSizeMake (scaledSize. height * imageFactor, scaledSize. height) ;}}/* turn the image to an upward direction */-(UIImage *) fixOrientation {if (self. imageOrientation = UIImageOrientationUp) {return self;} uigraphicsbeginimagecontextwitexceptions (self. size, NO, self. scale); [self drawInRect: CGRectMake (0.0, 0.0, self. size. width, self. size. height)]; UIImage * fixedImage = crop (); UIGraphicsEndImageContext (); return fixedImage;}/* compressedImageWithSize (CGSize) compressedImageWithSize () in ScaleToFit Mode) compressedSize {if (CGSizeEqualToSize (self. size, CGSizeZero) | (self. size. width <= compressedSize. width & self. size. height <= compressedSize. height) {// return self;} CGSize scaledSize = [self sizeOfScaleToFit: compressedSize]; // adjust the compression size to UIGraphicsBeginImageContext (scaledSize); [self drawInRect: CGRectMake (0.0, 0.0, scaledSize. width, scaledSize. height)]; UIImage * compressedImage = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); return compressedImage;} @ end
5. UIView
@ Interface UIView (MyCategory)-(void) startShakeAnimation; // shake animation-(void) animation;-(void) startRotateAnimation; // 360 ° rotate animation-(void) stopRotateAnimation; ///-(UIImage *) screenshot; @ property (nonatomic) float top; @ property (nonatomic) float bottom; @ property (nonatomic) float left; @ property (nonatomic) float right; @ end
@implementation UIView (MyCategory)/**/- (UIImage *)screenshot{ UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 2.0); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image;}#pragma mark - Animation- (void)startShakeAnimation{ CGFloat rotation = 0.05; CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"transform"]; shake.duration = 0.2; shake.autoreverses = YES; shake.repeatCount = MAXFLOAT; shake.removedOnCompletion = NO; shake.fromValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform, -rotation, 0.0, 0.0, 1.0)]; shake.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform, rotation, 0.0, 0.0, 1.0)]; [self.layer addAnimation:shake forKey:@"shakeAnimation"];}- (void)stopShakeAnimation{ [self.layer removeAnimationForKey:@"shakeAnimation"];}- (void)startRotateAnimation{ CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"transform"]; shake.duration = 0.5; shake.autoreverses = NO; shake.repeatCount = MAXFLOAT; shake.removedOnCompletion = NO; shake.fromValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform, M_PI, 0.0, 0.0, 1.0)]; shake.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform, 0.0, 0.0, 0.0, 1.0)]; [self.layer addAnimation:shake forKey:@"rotateAnimation"];}- (void)stopRotateAnimation{ [self.layer removeAnimationForKey:@"rotateAnimation"];}-(float)top{ return self.frame.origin.x;}-(float)bottom{ return self.top+self.frame.size.height;}-(float)left{ return self.frame.origin.x;}-(float)right{ return self.left+self.frame.size.width;}@end