IOS Common category category share

Source: Internet
Author: User
IOS Common category category shareWords 1340 Read 86 comments 2 like 8 Preface

As you know, categories are a way to add new methods to existing classes, using the dynamic Run-time allocation mechanism of OBJECTIVE-C, you can add new methods to existing classes, the way you add new methods to existing classes, called Category Catagory, and he can add new methods to any class. Include classes that do not have source code. Categories allow you to do the same work without creating subclasses of object classes.
There are a number of small technical points that are directly implemented by categories to save time and focus on more important development tasks, and now I'm sharing the category I've accumulated since iOS was developed. The first part nsstring, nsattributedstring nsstring include:
1.png 1.nsstring+category:

Some NSString general content

Telephone Number Center 4-bit * * * * * * display
+ (nsstring*) Getsecrectstringwithphonenumber: (nsstring*) phonenum;

Bank card Number Center 8-digit Display
+ (nsstring*) Getsecrectstringwithaccountno: (nsstring*) Accountno;

Calculated text height
-(cgfloat   ) Heightwithfontsize: (cgfloat) fontsize width: (cgfloat) width;

/** erases the 0*/
-(NSString *) Removeunwantedzero of the fractional end of the freight;

Remove before and after the space
-(NSString *) trimmedstring;

The data type is converted to Base64
+ (NSString *) Base64stringfromdata: (NSData *) data length: (nsuinteger) length;

Telephone format
+ (nsstring*) Stringmobileformat: (nsstring*) mobile;

The Chinese format of the array (tens of thousands of) can be added by itself
+ (nsstring*) Stringchineseformat: (double) value;
2.nsstring+predicate:

Common Regular expression Judgments

Valid telephone number
-(BOOL) Isvalidmobilenumber;

Valid real name
-(BOOL) isvalidrealname;

Whether only Chinese
-(BOOL) Isonlychinese;

Valid verification code (modified according to the number of verification code of your own)
-(BOOL) Isvalidverifycode;

Valid bank card number
-(BOOL) Isvalidbankcardnumber;

Valid mailbox
-(BOOL) IsValidEmail;

Valid alphanumeric password
-(BOOL) Isvalidalphanumberpassword;

Detection of valid ID
//15
-(BOOL) Isvalididentifyfifteen;

18
-bit-(BOOL) Isvalididentifyeighteen;

Restrictions can only be entered in numbers
-(BOOL) Isonlynumber;
3.nsstring+displaytime:

Shift from timestamp to display time

Time difference (hours ago, days ago)
+ (NSString *) Comparecurrenttime: (nstimeinterval) comparedate by timestamp calculation;

The time stamp is used to draw the display time
+ (NSString *) Getdatestringwithtimestamp: (nstimeinterval) timestamp;

Display time by timestamp and format
+ (NSString *) Getstringwithtimestamp: (nstimeinterval) Timestamp formatter: (nsstring*) Formatter ;
nsattributedstring include:
2.png


Rich Text that blends text height, text, and pictures

Because the system calculates rich text's height is incorrect, oneself wrote the Method-(CGFloat) Heightwithcontainwidth: (cgfloat) width;
                                           Direct return nsattributedstring + (nsattributedstring *) attributestringwithprefixstring: (NSString *) prefixString

Suffixstring: (NSString *) suffixstring; /** * Direct Return nsattributedstring * * * @param prefixstring the string * @param prefixfont Font size * @param prefixcolor Word Body color * @param suffixstring String * @param suffixfont Font size * @param suffixcolor Font Color * * @return return directly to Nsatt
                                             Ributedstring */+ (nsattributedstring *) attributestringwithprefixstring: (NSString *) prefixstring Prefixfont: (cgfloat) Prefixfont Prefixcolor: (UInt32) Prefi
                                             Xcolor suffixstring: (NSString *) suffixstring

Suffixfont: (cgfloat) Suffixfont Suffixcolor: (UInt32) Suffixcolor; StRing in front picture in Rear + (nsmutableattributedstring *) attributestringwithprefixstring: (NSString *) prefixstring

Subffiximagename: (NSString *) subffiximagename;
                                                Picture in the former string at the rear + (nsmutableattributedstring *) attributestringwithsubffixstring: (NSString *) subffixstring Prefiximagename: (NSString *) prefiximagename;
Part II Nsarray,nsdictionary

This piece of content is mainly to prevent the nil of the situation, variable and immutable encounter nil and the situation of the flash back to achieve Nsarray, Nsmutablearray

@interface Nsarray (Category)
//The following notation prevents Flicker
+ (instancetype) Safearraywithobject: (ID) object;

-(ID) Safeobjectatindex: (nsuinteger) index;

-(Nsarray *) Safesubarraywithrange: (nsrange) range;

-(Nsuinteger) Safeindexofobject: (ID) anobject;

The array
+ (Nsarray *) arraynamed: (NSString *) name is fetched through the plist name to the plist file;

The array is converted to a JSON string
-(NSString *) Tojsonstringforarray;
@end

@interface Nsmutablearray (Category)
//The following notation prevents Flash
-(void) Safeaddobject: (ID) object;

-(void) Safeinsertobject: (ID) object Atindex: (nsuinteger) index;

-(void) Safeinsertobjects: (Nsarray *) objects Atindexes: (Nsindexset *) Indexs;

-(void) Saferemoveobjectatindex: (nsuinteger) index;

-(void) Saferemoveobjectsinrange: (nsrange) range;
@end
nsdictionary, Nsmutabledictionary
@interface nsdictionary (Category)

//For data parsing, return object is string or value type, array and dictionary do not use this method
-(ID) Safeobjectforkey: (NSString *) Key;

Sets the key-value pair to NULL for the object
-(void) Safesetobject: (ID) object forkey: (ID) key;
-(ID) Objectforkeycustom: (ID) akey;

-(ID) Safekeyforvalue: (id) value;

/**
 *  field to JSON String * *  @return JSON string
/-(NSString *) tojsonstringfordictionary ;
@end

@interface nsmutabledictionary (Category)
-(void) Safesetobject: (ID) aobj Forkey: (id<nscopying >) Akey;

-(ID) Safeobjectforkey: (id<nscopying>) Akey;
@end
The third part uiview, UIImage, UIButton UIView
Uimotioneffect and home page background parallax effect
-(void) Addcentermotioneffectsxywithoffset: (cgfloat) offset;

Add view to Window
-(void) Addtowindow;

View screenshot
-(uiimage*) screenshot;

ScrollView screenshot Contentoffset
-(uiimage*) Screenshotforscrollviewwithcontentoffset: (Cgpoint) ContentOffset;

View Press Rect screenshot
-(uiimage*) Screenshotinframe: (CGRect) frame;
UIImage, here are a lot of things I only list a part of the specific people can down or clone down after careful look
Generate pictures by color
+ (UIImage *) Imagewithcolor: (uicolor*) color;

Trim the picture to the target size
+ (UIImage *) Imagebyscalingandcroppingforsourceimage: (UIImage *) sourceimage targetsize: (cgsize) Targetsize;

Picture Rotation angle
-(UIImage *) Imagerotatedbydegrees: (cgfloat) degrees;

Stretch picture Uiedgeinsets
-(UIImage *) Resizableimage: (uiedgeinsets) insets;

Stretch picture CGFloat
-(UIImage *) Imagebyresizetoscale: (cgfloat) scale;

Enlarge picture Cgsize
-(UIImage *) Imagebyresizewithmaxsize: (cgsize) size;

Sample picture Cgsize
-(UIImage *) Imagewiththumbnailforsize: (cgsize) size;

Trim picture through Rect
-(UIImage *) Imagebycroptorect: (cgrect) rect;
UIButton, enlarge the range of clicks, a very useful method, strongly recommend
-(void) Setenlargeedgewithtop: (cgfloat) Top right: (cgfloat) Right bottom: (cgfloat) bottom left: (CGFloat) left;
Summary

This side of some commonly used categories for you to enumerate some, in fact, there are many other categories on the GitHub project, we can carefully study, after all, the app has a lot of features are common, basically every method is I write because of some needs, and some of the online search. If you can not use to ask me, the first time to write a share of the article, do not like to spray. Thank you
I will continue to update it later. Finally, release the transmission door. Trouble to download the classmate to point a star or point a praise ~ (≧▽≦)/~ Thank you. Download Address

git download Address

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.