Share some of IOS development practical tips _ios

Source: Internet
Author: User
Tags json webp

1. Set Navigationbar title color

 Uicolor *whitecolor = [Uicolor whitecolor];
Nsdictionary *dic = [nsdictionary dictionarywithobject:whitecolor forkey:nsforegroundcolorattributename];
[Self.navigationController.navigationBar Settitletextattributes:dic];

2. Get Uicolor RGB

 Uicolor *color = [Uicolor colorwithred:0.0 green:0.0 blue:1.0 alpha:1.0];
Const CGFloat *components = cgcolorgetcomponents (color. Cgcolor);
NSLog (@ "Red:%f", components[0]);
NSLog (@ "Green:%f", components[1]);
NSLog (@ "Blue:%f", components[2]);
NSLog (@ "Alpha:%f", components[3]);

3. Modify the font color, size of TextField placeholder

 [Self.textfield setvalue:[uicolor Redcolor] forkeypath:@ "_placeholderlabel.textcolor"];
[Self.textfield Setvalue:[uifont boldsystemfontofsize:16] forkeypath:@ "_placeholderlabel.font"];

4. Convert Color to UIImage

 -(UIImage *) Createimagewithcolor: (Uicolor *) color {
 CGRect rect = CGRectMake (0.0f, 0.0f, 1.0f, 1.0f);
 Uigraphicsbeginimagecontext (rect.size);
 Cgcontextref context = Uigraphicsgetcurrentcontext ();
 Cgcontextsetfillcolorwithcolor (context, [color Cgcolor]);
 Cgcontextfillrect (context, rect);
 UIImage *theimage = Uigraphicsgetimagefromcurrentimagecontext ();
 Uigraphicsendimagecontext ();
 return theimage;
}

5. Hide StatusBar when loading a startup diagram

Join status bar is initially hidden set to Yes in Info.plist

6. Get the size of the button title

/**
 * Get button title of size * * *
-(CGFloat) Getbtntitlewidth: (uibutton*) btn {cgsize titlesize
 = [ Btn.titleLabel.text Sizewithattributes:@{nsfontattributename:btn.titlelabel.font}];
 return titlesize;
}

7. Set the status bar color

UIView *view = [[UIView alloc] Initwithframe:cgrectmake (0, -20, ScreenWidth, 20)]; [View Setbackgroundcolor:color_app_main];
[ViewController.navigationController.navigationBar Addsubview:view];

8.json Dictionary,dictionary Turn JSON

+ (nsstring*) Dictionarytojson: (Nsdictionary *) dic {

 Nserror *parseerror = nil;

 NSData *jsondata = [nsjsonserialization datawithjsonobject:dic options:nsjsonwritingprettyprinted error:& ParseError];

 return [[NSString alloc] Initwithdata:jsondata encoding:nsutf8stringencoding];

}
+ (Nsdictionary *) Jsontodic: (nsstring*) jsonstr {nsdata *jsondata
  = [Jsonstr datausingencoding: Nsutf8stringencoding];
  Nserror *err;
  Nsdictionary *dic = [nsjsonserialization jsonobjectwithdata:jsondata
               options:nsjsonreadingmutablecontainers
                error:&err];
  return dic;
}

9. Whether to allow push

+ (BOOL) isallowednotification{

 if ([[Uidevice currentdevice].systemversion Floatvalue] >= 8.0) {

  Uiusernotificationsettings *setting = [[UIApplication sharedapplication] currentusernotificationsettings];

  if (Uiusernotificationtypenone!= setting.types) {return

   YES
  }
 }
 NSLog (@ "not allowed to push");
 return NO;
}

10. Disk space-related

 + (NSString *) Memoryformatter: (Long Long) diskspace {nsstring *formatted;
 Double bytes = 1.0 * DISKSPACE;
 Double megabytes = BYTES/MB;
 Double gigabytes = BYTES/GB;
 if (gigabytes >= 1.0) formatted = [NSString stringwithformat:@ "%.2f GB", gigabytes];
 else if (Megabytes >= 1.0) formatted = [NSString stringwithformat:@ "%.2f MB", megabytes];
 else formatted = [nsstring stringwithformat:@ "%.2f bytes", bytes];

 NSLog (@ "fotmatted=%@", formatted);
return formatted; } + (NSString *) Totaldiskspace {long Long space = [[[Nsfilemanager Defaultmanager] Attributesoffilesystemforpath:nsho
 Medirectory () Error:nil] objectforkey:nsfilesystemsize] longlongvalue];
return [self memoryformatter:space]; } + (NSString *) freediskspace {long Long freeSpace = [[[Nsfilemanager Defaultmanager] Attributesoffilesystemforpath:n
 Shomedirectory () Error:nil] objectforkey:nsfilesystemfreesize] longlongvalue];
return [self memoryformatter:freespace]; }

11. Modified Leftbarbuttonitem How to restore the system sideslip return function

 Set Agent
self.interactivePopGestureRecognizer.delegate = self;
#pragma mark-<UIGestureRecognizerDelegate>
/Implementation Proxy method: Return YES: Gesture valid, NO: Gesture Invalid
-(BOOL) Gesturerecognizershouldbegin: (Uigesturerecognizer *) Gesturerecognizer
{
 //When the number of sub controllers in the navigation controller is greater than 1 gestures is valid
 return self.childViewControllers.count > 1;
}

Or with a third partyUINavigationController+FDFullscreenPopGesture

12. Use uiappearance in a certain state to set the color, font and so bad

You just need to refresh it in the corresponding position layoutIfNeeded .

13. Set the circular picture

/** set a circular picture (to be used in the classification)/
-(UIImage *) cutcircleimage {
 uigraphicsbeginimagecontextwithoptions (self.size, NO, 0.0);
 Gets the context
 cgcontextref ctr = Uigraphicsgetcurrentcontext ();
 Set round
 CGRect rect = cgrectmake (0, 0, self.size.width, self.size.height);
 Cgcontextaddellipseinrect (CTR, rect);
 Cutting
 Cgcontextclip (CTR);
 Draw the picture up
 [self drawinrect:rect];
 UIImage *image = Uigraphicsgetimagefromcurrentimagecontext ();
 Uigraphicsendimagecontext ();
 return image;
}

14. If there is a control in the Xib, the size has been clearly set, the output frame is also true, but the results show that the effect is not the same (such as the size of the larger), if this is the case is generally autoresizingmask automatic scaling properties in mischief!

The solution is as follows:

Xib awakefromnib method to set Uiviewautoresizingnone to empty

 -(void) awakefromnib {
 Self.autoresizingmask = Uiviewautoresizingnone;
}

15. Get the picture extension by the first byte of the image data

-(NSString *) Contenttypeforimagedata: (NSData *) data {
 uint8_t C;
 [Data getbytes:&c length:1];
 Switch (c) {case
  0xFF: Return
   @ "JPEG";
  Case 0X89: Return
   @ "PNG";  
  Case 0X47: Return
   @ "GIF";  
  Case 0x49: Case 
  0x4d: Return
   @ "TIFF";  
  Case 0X52: 
   if ([Data length] < "{return
    nil;
   }
   NSString *teststring = [[NSString alloc] Initwithdata:[data subdatawithrange:nsmakerange (0)] Encoding: Nsasciistringencoding];
   if ([teststring hasprefix:@ "RIFF"] && [teststring hassuffix:@ "WEBP"]) {return
    @ "WEBP";
   }
   return nil;
 }
 return nil;
}

16.0-Complement method

Nsinteger count = 5;
02 represents: If Count is less than 2 digits with 0 in the front complement (2 represents the total number of outputs)
nsstring *string = [NSString stringwithformat:@ "%02zd", Count];
The output result is:
NSLog (@ "%@", string);

Summarize

The above is the entire content of this article, I hope this article of these tips can give you the development of iOS when providing a certain amount of help, if you have questions you can message exchange.

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.