We found that slowly accumulated a lot of their own written in various categories.
。 Today, selfless. 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;//distance from current time interval-(NSString *) minutedescription;// Accurate to minute date-(NSString *) formatteddatedescription;//formatted date @end
@implementation NSDate (mycategory)-(NSString *) timeintervaldescription{nstimeinterval timeinterval =-[self time Intervalsincenow];if (TimeInterval <) {return @ "within 1 minutes";} else if (TimeInterval < 3600) {return [NS] String stringwithformat:@ "%.f minutes Ago", TIMEINTERVAL/60];} else if (TimeInterval < 86400) {return [NSString stringwithformat:@ "%.f hours Ago", timeinterval/3600];} else if (Ti Meinterval < 2592000) {//30 days return [NSString stringwithformat:@ "%.f days Ago", timeinterval/86400]; } else if (TimeInterval < 31536000) {//30 days to 1 years nsdateformatter *dateformatter = [NSDateFormatter dateformatterwi thformat:@ "M month D Day"]; 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];//Date Month Day nsstring *currentday = [dateformatter stringfromdate:[nsdate date]];//Current Month day if ([Theday Isequaltostring:curr Entday]) {//day [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) {//interval within one week [dateformatter setdateformat:@ "Eeee ah:mm"]; return [Dateformatter stringfromdate:self]; } else {//once [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];//date Month Day nsstring *currentday = [Dateformatter stringFromDate:[ NSDate date]];//Current Month day Nsinteger timeinterval =-[self Timeintervalsincenow]; if (TimeInterval <) {return @ "within 1 minutes";} else if (TimeInterval < 3600) {//1 hours return [NSString Strin gwithformat:@ "%d minutes ago", TIMEINTERVAL/60];} else if (TimeInterval < 21600) {//6 hours return [NSString stringwithformat:@ "%d hours ago", timeinterval/3600];} else I f ([Theday Isequaltostring:currentday]) {///day [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 {//once [Dateformatter setdateformat:@ "Mm-dd hh:mm"]; return [Dateformatter stringfromdate:self];}} @end
3.UIColor
@interface Uicolor (mycategory) + (Uicolor *) Colorwithhexrgb: (NSString *) hexrgbstring; @end
@implementation Uicolor (mycategory)/* 16 binary string Gets the color */+ (Uicolor *) Colorwithhexrgb: (NSString *) hexrgbstring{ if ([ Hexrgbstring hasprefix:@ "#"]) { hexrgbstring = [hexrgbstring substringfromindex:1]; } unsigned int colorcode = 0; unsigned char redbyte, greenbyte, Bluebyte; if (hexrgbstring) { Nsscanner *scanner = [Nsscanner scannerwithstring:hexrgbstring]; [Scanner scanhexint:&colorcode]; } Redbyte = (unsigned char) (ColorCode >>); 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)//Picture stretching, tiling interface-(UIImage *) Resizableimagewithcompatiblecapinsets: (uiedgeinsets) capinsets Resizingmode: (uiimageresizingmode) resizingmode;//image Scaletofit-stretched cgsize-(cgsize) Sizeofscaletofit: (CGSize) scaledsize;//adjust the picture to upward-(UIImage *) fixorientation;//compress the picture in Scaletofit way-(UIImage *) Compressedimagewithsize: (cgsize ) compressedsize; @end
@implementation UIImage (mycategory)/* Gets the specified picture of the current theme package */+ (UIImage *) themeimagenamed: (NSString *) name{Nsuinteger Themeid = [[Viewmanager Defaultmanager] Themeid]; if (Themeid = = 0) {//default theme NSString *filename = [NSString stringwithformat:@ "lianxi.bundle/images/%@", name]; return [self imagenamed:filename]; } else {//download topic return nil; }}/* picture stretched, tiled interface. Compatible with ios5+*/-(UIImage *) Resizableimagewithcompatiblecapinsets: (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 Stretchab LeImageWithLeftCapWidth:capInsets.left TopCapHeight:capInsets.top]; } else {//uiimageresizingmodetile return [self resizableimagewithcapinsets:capinsets]; }} else {return [self stretchableImageWithLeftCapWidth:capInsets.left topCapHeight:capInsets.top]; }}/* picture Scaletofit-Stretched cgsize*/-(cgsize) Sizeofscaletofit: (cgsize) scaledsize{cgfloat scalefactor = Scaledsize.width/scaledsize.height; CGFloat imagefactor = self.size.width/self.size.height; if (scalefactor <= imagefactor) {//Picture horizontally fills return Cgsizemake (Scaledsize.width, scaledsize.width/imagefactor); } else {//Vertical fill return cgsizemake (scaledsize.height * imagefactor, scaledsize.height); }}/* turn the picture to the upward */-(UIImage *) fixorientation{if (self.imageorientation = = Uiimageorientationup) {return self; } uigraphicsbeginimagecontextwithoptions (Self.size, NO, Self.scale); [Self drawinrect:cgrectmake (0.0, 0.0, self.size.width, self.size.height)]; UIImage *fixedimage = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); return fixedimage; }/* Compress pictures in scaletofit mode */-(UIImage *) compressEdimagewithsize: (cgsize) compressedsize{if (Cgsizeequaltosize (self.size, Cgsizezero) | | (Self.size.width <= compressedsize.width && self.size.height <= compressedsize.height)) {//Do not compress return self; } cgsize scaledsize = [self sizeofscaletofit:compressedsize]; The compression size. Adjust steering 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) stopshakeanimation;-(void) Startrotateanimation;//360° Rotation 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
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
iOS frequently use categories