iOS Summary: Various small pits in the project summary

Source: Internet
Author: User
Tags string to json

One, string to JSON

In the case of a network request, if the server returns a string, then we need to encapsulate a class ourselves and convert the requested string into a JSON object to be stored in the model.

Note: If you have some special characters (such as \ n, \ t, etc.) in a string, you need to first process the string.

The sample code is as follows:

+ (Nsdictionary *) dictionarywithjsonstring: (NSString *) jsonstring{if(jsonstring = =Nil) {    returnNil;} Jsonstring= [Jsonstring stringbyreplacingoccurrencesofstring:@"\\r"Withstring:@""];jsonstring= [Jsonstring stringbyreplacingoccurrencesofstring:@"\\n"Withstring:@""];jsonstring= [Jsonstring stringbyreplacingoccurrencesofstring:@"\\s"Withstring:@""];jsonstring= [Jsonstring stringbyreplacingoccurrencesofstring:@"\\t"Withstring:@""];jsonstring= [Jsonstring stringbyreplacingoccurrencesofstring:@"\\v"Withstring:@""];jsonstring= [Jsonstring stringbyreplacingoccurrencesofstring:@"\\f"Withstring:@""];jsonstring= [Jsonstring stringbyreplacingoccurrencesofstring:@"\\b"Withstring:@""];jsonstring= [Jsonstring stringbyreplacingoccurrencesofstring:@"\\a"Withstring:@""];jsonstring= [Jsonstring stringbyreplacingoccurrencesofstring:@"\\e"Withstring:@""]; NSData* Jsondata =[jsonstring datausingencoding:nsutf8stringencoding]; Nserror*err; Nsdictionary* dic = [nsjsonserialization jsonobjectwithdata:jsondata options:nsjsonreadingmutablecontainers error:&err];if(Err) {Ysxlog (@"JSON parse failed:%@", err); returnNil;}returnDiC;}

Second, the picture stretching
Uiimageview *RIGHTIMAGV =[[Uiimageview alloc]init]; UIImage* Img=[uiimage imagenamed:@"tu_text_values"];//OriginalUiedgeinsets Edge=uiedgeinsetsmake (5, Myscalwidth ( -),5, Myscalwidth ( -));//Uiimageresizingmodestretch: Stretch mode to fill a picture by stretching the rectangular area specified by the Uiedgeinsets//Uiimageresizingmodetile: Tile mode, fills the map by repeating the uiedgeinsets specified rectangular areaimg=[img Resizableimagewithcapinsets:edge resizingmode:uiimageresizingmodestretch];rightimagv.image=img; [RIGHTIMAGV sizetofit];rightimagv.width= Myscalwidth ( the) +scorel.width+myscalwidth ( -); rightimagv.x= Screen_width-myscalwidth (Ten)-Rightimagv.width;rightimagv.centery=Cgrectgetmidy (prov.frame); [TopView addsubview:rightimagv];scorel.x= Myscalwidth ( the); Scorel.centery= rightimagv.height*0.5; [RIGHTIMAGV Addsubview:scorel];

Third, label text adaptive frame

Way One

It is recommended this way to get the height, to achieve line wrapping, leading settings

UILabel * infolab=[[UILabel alloc] init];//Infolab.text=self.infotext; Infolab.font=[uifont Systemfontofsize:myscalfont ( -)]; Infolab.textcolor=rgb (102,102,102,1); Infolab.numberoflines=0; Nsmutableattributedstring*INFOSTR = [hp_nstring createAttributeStringWithText:self.infoText linespace:myscalheight ( A) AndFont:infoLab.font AndColor:infoLab.textColor]; Infolab.attributedtext=Infostr; Cgsize infosize= [hp_nstring sizeOfText:self.infoText withFont:infoLab.font Andsize:cgsizemake (Bgview.valueofw-myscalwidth ( A)*2, +) Andlinespace:myscalheight ( A) AndColor:infoLab.textColor]; Infolab.width=Infosize.width; Infolab.height=Infosize.height; infolab.x=TYPELAB.VALUEOFX; Infolab.y=typelab.valueofbottommargin+myscalheight ( -); [Self.view Addsubview:infolab];

Way Two

CGFloat detailinfolabelx=Cgrectgetmidx (questimageview.frame); CGFloat DETAILINFOLABELW=detailinfoview.width-detailinfolabelx*2; UILabel* Detailinfolabel=[[uilabel alloc] init]; detailinfolabel.numberoflines=0; Detailinfolabel.text=@"Cheerleader, La la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la la  La , LA, la la, no, no, no, no."; Detailinfolabel.textcolor=rgb (102,102,102,1); Detailinfolabel.font=[uifont Systemfontofsize:myscalfont ( -)]; Cgsize detailsize=[Detailinfolabel.text SizeWithFont:detailInfoLabel.font constrainedtosize:cgsizemake (DETAILINFOLABELW,    maxfloat) linebreakmode:nslinebreakbycharwrapping]; Detailinfolabel.x=Detailinfolabelx; Detailinfolabel.y=0; Detailinfolabel.width=Detailsize.width; Detailinfolabel.height=Detailsize.height; [Detailinfoview Addsubview:detailinfolabel];

Four, the time interval one day

Requirements in the project: Control pop-up times, require pop-up once a day, write a class, easy to call

+(void) JUMPTOVC: (Uiviewcontroller *) MYVC Withsaveparam: (NSString *) Saveparam withsavedate: (nsdate *) saveDate Withnavigationcontroller: (Uinavigationcontroller *) nav{//determine if the parameter is savedif(saveparam.length>0&& Saveparam! = nil) {//YYsxlog (@"parameter is saved");}Else{//N//determine if time is saved    if(Savedate! = nil) {//Y//Judging if it's more than 24 hours        if([[NSDate Date] timeintervalsincedate:savedate]/3600> -) {//more than 24 hours[nav PUSHVIEWCONTROLLER:MYVC animated:yes]; }Else{Ysxlog (@"no more than 24 hours"); }    }Else{//N Jump[nav PUSHVIEWCONTROLLER:MYVC animated:yes]; }}}

Call, because "depends on the interface" has not been loaded, so sometimes do not eject successfully, can appropriately delay the pop-up time of 1 seconds

Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1 * nsec_per_sec)), Dispatch_get_main_queue (), ^ {    * vc = [[Emailviewcontroller alloc] init];    [YSXJUMPTOVC jumptovc:vc withsaveparam:[ysxuserinfo sharedysxuserinfo].addemail withSaveDate:[YSXUserInfo Sharedysxuserinfo].addemaildate WithNavigationController:self.navigationController];});

A comparison of five or two dates

Returns two times from the server as a string, requiring a comparison of the two sizes

NSDateFormatter * DF =[[NSDateFormatter alloc] init]; [DF Setdateformat:@"YYYY-MM-DD HH:mm:ss"]; NSDate* DT1 =[[NSDate alloc] init]; NSDate* DT2 =[[NSDate alloc] init]; //time to sign inDT1 =[DF Datefromstring:[ysxuserinfo SHAREDYSXUSERINFO].COMENTTIMESTR1]; //get time in real timeDT2 =[DF Datefromstring:[ysxuserinfo SHAREDYSXUSERINFO].COMENTTIMESTR]; Nscomparisonresult result=[Dt1 COMPARE:DT2]; if(Result = = nsordereddescending) {//DT1>DT2Redview.hidden =NO; }Else{Redview.hidden=YES; }//when DT1 is greater than DT2, the result is nsordereddescending//when Dt1 equals DT2, the result is Nsorderedsame//when DT1 is less than DT2, the result is nsorderedascending

VI. UIView add shadow effect is invalid

To fillet the view around the effect of the shadow, the result of a half day did not get out, the original is my view round the corner, in addition to the View.layer.cornerRadius settings, the back is always used to add View.layer.masksToBounds = YES, Cut the shadow, of course not.

Seven, hide the status bar

In general, when we create the interface system will be set aside 20px blank to the top of the status bar, but this blank is not good to see Ah, So we add self.automaticallyadjustsscrollviewinsets = NO to the Viewdidload method in the corresponding controller, and [[UIApplication sharedapplication] Setstatusbarhidden:yes
Withanimation:uistatusbaranimationfade]; is to hide the entire status bar, the user experience is not good, here is not advocated. When I deal with the status bar found Automaticallyadjustsscrollviewinsets method does not work, after the Internet query, the final solution, the original controller I will scrollview as the first view, As long as ScrollView's first view is canceled, the Automaticallyadjustsscrollviewinsets method works.

OK, today to summarize these points, the wrong place, I hope the great God more guidance!??????

iOS Summary: Various small pits in the project summary

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.