Common code collation (important)

Source: Internet
Author: User

Common Code collation:

1. Determine if the mailbox format is correct:Using regular expression validation-(BOOL) Isvalidateemail: (NSString *) email{nsstring *emailregex = @ "[A-z0-9a-z._%+-][email protected][ A-za-z0-9.-]+\\. [A-za-z] {2,4} "; Nspredicate *emailtest = [Nspredicate predicatewithformat:@ "Self matches%@", Emailregex]; return [Emailtest Evaluatewithobject:email];}2. Image compressionUsage: UIImage *yourimage= [self imagewithimagesimple:image scaledtosize:cgsizemake (210.0, 210.0)];//compress picture-(uiimage*) Imagewithimagesimple: (uiimage*) Image scaledtosize: (cgsize) newsize{//Create a graphic text Uigraphicsbeginimagecontext (newSize  );  Draw the dimensions of the new text//New size[image Drawinrect:cgrectmake (0,0,newsize.width,newsize.height)]; Get a new picture from the text uiimage* newimage = Uigraphicsgetimagefromcurrentimagecontext (); End Edit Text Uigraphicsendimagecontext (); Return the new Image.return NewImage;}3. Pro-Test the available image upload code//Button response event-(Ibaction) Uploadbutton: (ID) Sender {uiimage *image = [uiimage imagenamed:@ "1.jpg"];//Picture name nsdata * ImageData = uiimagejpegrepresentation (image,0.5);//Compression ratio NSLog (@ "number of bytes:%i", [imageData length]);//Post Urlnsstring *urlstring = @ "Http://192.168.1.113:8090/text/UploadServlet";//server address//setting up the request Object nownsmutableurlrequest *request = [[Nsmutableurlrequest alloc] init]; [request seturl:[nsurl urlwithstring:urlstring]]; [request sethttpmethod:@ "POST"];//nsstring *boundary = [nsstring stringwithstring:@ "------------- --------------14737809831466499882746641449 "]; Nsstring *contenttype = [nsstring stringwithformat:@ "multipart/form-data;boundary=%@", boundary]; [request addvalue:contenttype forhttpheaderfield: @ "content-type"];//NSMutableData *body = [ nsmutabledata data]; [body appenddata:[[nsstring stringwithformat:@ "\r\n--%@\r\n", boundary] datausingencoding: Nsutf8strinGencoding]]; [body appenddata:[[nsstring stringwithstring:@ "Content-disposition:form-data; name=\" Userfile\ "; Filename=\ "2.png\" \ r \ n "] datausingencoding:nsutf8stringencoding]]; Uploaded image name [body appenddata:[[nsstring stringwithstring:@] content-type:application/octet-stream\r\n\ r\n "] datausingencoding:nsutf8stringencoding]]; [body appenddata:[nsdata datawithdata:imagedata]]; [body appenddata:[[nsstring stringwithformat:@ "\r\n--%@--\r\n", boundary] datausingencoding: nsutf8stringencoding]];  [request sethttpbody:body]; //NSLog (@ "1-body:%@", body); NSLog (@ "2-request:%@", request);  nsdata *returndata = [Nsurlconnection sendsynchronousrequest: request returningresponse:nil error:nil]; nsstring *returnstring = [[nsstring alloc] initwithdata:returndata encoding: Nsutf8stringencoding]; NSLog (@ "3-Test output:%@", returnstring); 4. Operations on the gallery Select album:uiimagepickercontrollersourcetypesourcetype=uiimagepickercontrollersourcetypecamera;   if (![ Uiimagepickercontrollerissourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {       sourcetype=uiimagepickercontrollersourcetypephotolibrary;  }   Uiimagepickercontroller * Picker = [[uiimagepickercontrolleralloc]init];   Picker.delegate = self;    picker.allowsediting=yes;   picker.sourcetype=sourcetype;   [self Presentmodalviewcontroller:picker Animated:yes]; Selection complete:  -(void) Imagepickercontroller: ( uiimagepickercontroller*) Pickerdidfinishpickingmediawithinfo: (nsdictionary *) info{   [picker dismissmodalviewcontrolleranimated:yes];   UIImage * Image=[info Objectforkey: uiimagepickercontrollereditedimage];   [Self performselector: @selector (selectpic:) Withobject: imageafterdelay:0.1];}  -(void) Selectpic: (uiimage*) image{   NSLog (@ "image%@", image);     ImageView = [[Uiimageview alloc] initwithimage:image];   imageview.frame = CGRectMake (0, 0, Image.size.width, Image.size.height); [self.viewaddsubview:imageview];   [Self Performselectorinbackground: @selector (Detect:) Withobject:nil] ;} Detect the method that you define for yourself, edit the effect you want to achieve after selecting the photo deselect:

-(void) Imagepickercontrollerdidcancel: (uiimagepickercontroller*) Picker

{[Picker dismissmodalviewcontrolleranimated:yes];} 5, create a Uibarbuttonitem right buttonUibarbuttonitem *rightbutton = [[Uibarbuttonitem alloc] initwithtitle:@ "right" Style:uibarbuttonitemstyledone target: Self action: @selector (Clickrightbutton)]; [Self.navigationitem Setrightbarbuttonitem:rightbutton];6, set Navigationbar hideSelf.navigationController.navigationBarHidden = yes;//7,ios Development Uilabel Multi-line text wrapping (auto-fold lines)UIView *footerview = [[UIView alloc]initwithframe:cgrectmake (10, 100, 300, 180)]; UILabel *label = [[UILabel alloc]initwithframe:cgrectmake]];label.text = @ "Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Helloworld! "; Auto fold line Setting Label.linebreakmode = Uilinebreakmodewordwrap;label.numberoflines = 0;8, Code generation buttonCGRect frame = CGRectMake (0, 400, 72.0, 37.0); UIButton *button = [UIButton buttonwithtype:uibuttontyperoundedrect];button.frame = frame; [Button settitle:@ "newly added buttons" Forstate:uicontrolstatenormal];button.backgroundcolor = [Uicolor Clearcolor];button.tag = 2000; [Button addtarget:self action: @selector (buttonclicked:) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:button];9. Make a control appear in the center of the view:(a control, such as Label,view) Label.center = Self.view.center;

10. Good-looking word processing

Take the Textlabel of the cell in TableView as an example: Cell.backgroundcolor = [Uicolorscrollviewtexturedbackgroundcolor];
Set font for text
Cell.textLabel.font = [Uifont fontwithname:@ "Americantypewriter" size:100.0f];
Set the color of text
Cell.textLabel.textColor = [Uicolor Orangecolor];
Set the background color of text
Cell.textLabel.shadowColor = [Uicolor Whitecolor];
Set where text is displayed
Cell.textLabel.textAlignment = Uitextalignmentcenter;

11. Hide Status Bar
The reader may know an easy way to add a viewdidload to the program.

[[UIApplication Sharedapplication]setstatusbarhidden:yes Animated:no];

12. Change the Alertview background

Uialertview *thealert = [[[Uialertviewalloc] initwithtitle:@ "Atention"
Message: @ "I ' m a chinese!"
Delegate:nil
cancelbuttontitle:@ "Cancel"
otherbuttontitles:@ "Okay", Nil] autorelease];
[Thealert show];
UIImage *theimage = [uiimageimagenamed:@ "Lovechina.png"];
Theimage = [Theimage stretchableimagewithleftcapwidth:0topcapheight:0];
Cgsize thesize = [Thealert frame].size;
Uigraphicsbeginimagecontext (thesize);
[Theimage Drawinrect:cgrectmake (5, 5, thesize.width-10, thesize.height-20)];//the size of this place to adjust itself to fit the size of the alertview background color.
Theimage = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
TheAlert.layer.contents = (ID) [Theimage cgimage];

13, keyboard transparent
Textfield.keyboardappearance = Uikeyboardappearancealert;

14, the network activity of the status bar whether the wheel rotates
[UIApplication sharedapplication].networkactivityindicatorvisible, the default value is No.

15. Capture Screen Pictures
Creates a bitmap-based graphics context and specifies a size of Cgsizemake (200,400)
Uigraphicsbeginimagecontext (Cgsizemake (200,400));

Renderincontext renders the recipient and its child scopes to the specified context
[Self.view.layer Renderincontext:uigraphicsgetcurrentcontext ()];
Returns a picture based on the current drawing context
UIImage *aimage = Uigraphicsgetimagefromcurrentimagecontext ();
Removes the top of the stack based on the current bitmap's graphics context
Uigraphicsendimagecontext ();
Returns the data for the specified picture in PNG format
ImageData = Uiimagepngrepresentation (aimage);
16. Change the background of cell selection
UIView *myview = [[UIView alloc] init];
Myview.frame = CGRectMake (0, 0, 320, 47);
Myview.backgroundcolor = [Uicolorcolorwithpatternimage:[uiimage imagenamed:@ "0006.png"];
Cell.selectedbackgroundview = MyView;17. Can make the picture fit the box size (no confirmation)Nsstring*imagepath = [[NSBundle mainbundle] pathforresource:@ "Xcodecrash" oftype:@ "PNG"];
UIImage *image = [[UIImage Alloc]initwithcontentsoffile:imagepath];
UIImage *newimage= [Image transformwidth:80.f HEIGHT:240.F];
Uiimageview *imageview = [[Uiimageview alloc]initwithimage:newimage];
[Newimagerelease];
[Image release];
[Self.view Addsubview:imageview];

Common code collation (important)

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.