General macro definitions and header files for the app development process

Source: Internet
Author: User

工欲善其事, its prerequisite.

Before you formally implement all the cool features and UI, getting ready is the only way to improve the efficiency of your next development.

So, this series, I am not in a variety of heap technology, more attention is "Bing" before the "grain line", some cumbersome, but when the whole context is clear, the follow-up work is only on the good Foundation and framework of unlimited expansion and optimization.

Macro definition is an effective tool for improving efficiency in the development process. It is necessary to summarize some globally common macro definitions for easy and efficient use.

As below, some general macro definitions should be developed as necessary:

#pragmaMark--Set Global dimension macros#defineStatusbarheight 20#defineNavibarheight 44#defineTabbarheight 49#defineKeyboardengheight 216#defineKeyboardhansheight 252#pragmaMark--Set global color macros//the following macro definitions, which are used only as a device to adapt a reference scheme#defineStatusbarcolor [Uicolor Blackcolor]#defineNavibarcolor [Uicolor Whitecolor]#defineNavibartitleselectedcolor COLOR (255, 120, 100)#defineNavibartitleunselectedcolor COLOR (100, 100, 100)#defineNavibarshadowcolor COLOR (230, 230, 230)#defineNavibartitlefontsize ((Deviceisnotretina | | deviceisiphone4s | | DeviceIsiPhone5)? 17:19)#definenavibartitleattributes [Nsdictionary dictionarywithobjectsandkeys:fontappliedfixed (NaviBarTitleFontSize), NSFontA Ttributename,navibartitleunselectedcolor, Nsforegroundcolorattributename, nil]#defineNaviitemtextfontsize ((Deviceisnotretina | | deviceisiphone4s | | DeviceIsiPhone5)? 14:16)#defineNaviitemtextgraycolor COLOR (140, 140, 140)#defineNavibottomlinecolor Navibartitleselectedcolor#pragmaMark--String localization macro#defineLocalizedString (String) nslocalizedstring (string,string)#pragmaMark--Get device width and height//will vary with the app's internal horizontal and vertical screens#definedevicesize [UIScreen mainscreen].bounds.size#defineDevicewidth Devicesize.width#defineDeviceheight Devicesize.height#pragmaMark--Judge whether Iphone4,4s,5,6,plus,ipad//does not change with the app's internal horizontal and vertical screens#definedevicecurrentmodesize [UIScreen mainscreen].currentmode.size#defineDeviceportraitmodesize (Devicecurrentmodesize.width < devicecurrentmodesize.height? Devicecurrentmodesize:cgsizemake (Devicecurrentmodesize.height, Devicecurrentmodesize.width))#defineDeviceisnotretina cgsizeequaltosize (Deviceportraitmodesize, Cgsizemake (320, 480))#defineDeviceisiphone4s cgsizeequaltosize (Deviceportraitmodesize, Cgsizemake (640, 960))#defineDeviceIsiPhone5 cgsizeequaltosize (Deviceportraitmodesize, Cgsizemake (640, 1136))//6s and 6 same size#defineDeviceIsiPhone6 cgsizeequaltosize (Deviceportraitmodesize, Cgsizemake (750, 1334))//6s Plus 6Plus in the same size (some models are actually only 6s Bounds.size)#defineDeviceisiphone6plus (Cgsizeequaltosize (Deviceportraitmodesize, Cgsizemake (1125, 2001)) | | Cgsizeequaltosize (Deviceportraitmodesize, Cgsizemake (1242, 2208)))//debugging on the emulator, you may experience non-retina resolution#defineDeviceisipad (Cgsizeequaltosize (Deviceportraitmodesize, Cgsizemake (768, 1024)) | | Cgsizeequaltosize (Deviceportraitmodesize, Cgsizemake (1536, 2048)) | | Cgsizeequaltosize (Deviceportraitmodesize, Cgsizemake (1024, 1366)) | | Cgsizeequaltosize (Deviceportraitmodesize, Cgsizemake (2048, 2732)))#pragmaMark--Judging the device OS integer version number//the current system version number is still floating-point#definedeviceiosversion [[[Uidevice Currentdevice] systemversion] floatvalue]#defineDeviceiosversionis (x) (deviceiosversion >= x && deviceiosversion < x+1? Yes:no)#defineDeviceiosversionabove (x) (Deviceiosversion >= x? Yes:no)#pragmaMark-Calculates the current scaled dimension in the callout graph based on the device (callout chart version: 375 width) The ipad is displayed as standard IPhone6//the following macro definitions, which are used only as a device to adapt a reference scheme#defineResizeSideBase6 (s) (Deviceisipad s: (Devicewidth * s/375))#pragmaMark-Scales only according to the width of the design drawing (Callout chart version: 375 width)#defineResizeSideBase375 (s) (Devicewidth * s/375)#pragmaMark--Set a custom font#defineFontName1 @ "fzy3jw--gb1-0"//founder quasi-round TTF font name#defineFontName2 @ "Helvetica"#defineFontName3 @ "Helvetica-bold"//Fixed Font size#defineFontappliedfixed (n) [Uifont systemfontofsize:n]#defineFontappliedboldfixed (n) [Uifont boldsystemfontofsize:n]#defineFontfzzyfixed (n) [Uifont fontwithname:fontname1 size:n]#defineFonthelveticafixed (n) [Uifont fontwithname:fontname2 size:n]#defineFonthelveticaboldfixed (n) [Uifont Fontwithname:fontname3 size:n]//the following macro definitions, which are used only as a device to adapt a reference scheme//based on the device, calculate the display font (callout chart version: 375 width) The ipad is displayed as standard IPhone6#defineFONTAPPLIEDBASE6 (n) (deviceisiphone6plus?) Fontappliedfixed (n+0.5): (DeviceIsiPhone6 | | Deviceisipad? Fontappliedfixed (N): fontappliedfixed (n-1)))#defineFONTAPPLIEDBOLDBASE6 (n) (deviceisiphone6plus?) Fontappliedboldfixed (n+0.5): (DeviceIsiPhone6 | | Deviceisipad? Fontappliedboldfixed (N): fontappliedboldfixed (n-1)))#defineFONTHELVETICABASE6 (n) (deviceisiphone6plus?) Fonthelveticafixed (n+0.5): (DeviceIsiPhone6 | | Deviceisipad? Fonthelveticafixed (N): fonthelveticafixed (n-1)))#defineFONTHELVETICABOLDBASE6 (n) (deviceisiphone6plus?) Fonthelveticaboldfixed (n+0.5): (DeviceIsiPhone6 | | Deviceisipad? Fonthelveticaboldfixed (N): fonthelveticaboldfixed (n-1)))#pragmaMark--Load Picture macros (the following methods are frequently IO, do not cache pictures):#defineLoadImage (name) [UIImage imagewithcontentsoffile:[[nsbundle Mainbundle] Pathforresource:name Oftype:nil]#defineLoadimagewithtype (Name,type) [UIImage imagewithcontentsoffile:[[nsbundle Mainbundle] pathforresource:name ofType: Type]]#pragmaMark--Set 16 binary RGB colors (format: RRGGBB)#defineColorwithrrggbba (RRGGBB, A) [Uicolor colorwithred: ((float) ((RRGGBB & 0xFF0000) >>))/255.0 Green: ((float) ( (RRGGBB & 0xff00) >> 8)/255.0 Blue: ((float) (RRGGBB & 0xFF))/255.0 Alpha:a]#defineCOLORWITHRRGGBB (RRGGBB) Colorwithrrggbba (RRGGBB, 1.0)#pragmaMark--Set the 10 binary RGB color#defineColorwithrgba (R, G, B, A) [Uicolor colorwithred:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]#defineCOLOR (R, G, b) Colorwithrgba (R, G, B, 1.0)#defineColorwithimage (name) [Uicolor colorwithpatternimage:loadimage (name)]#pragmaMark--Angle radians conversion#defineDegreestoradian (x) (M_PI * x/180.0)#defineRadiantodegrees (Radian) (Radian * 180.0/m_pi)#pragmaMark--Log Output Control macro#ifdefDEBUG#defineLOG (...) NSLog (__va_args__);#defineLog_method NSLog (@ "%s", __func__);#else#defineLOG (...) ;#defineLog_method;#endif#pragmaMark--Weak reference macro definition#defineWS (weakself) __weak typeof (self) weakself = self;

The description is as follows:
1.[UIScreen mainscreen].bounds.size will change with the horizontal and vertical screen,[[UIScreen Mainscreen] currentmode].size will not change.

For example, the project general in the horizontal screen check, run on the iphone6s, the beginning of the vertical screen (375,667), the application rotated to the horizontal screen, the former width and height swapped (667,375); the latter is always (750,1334)

2.Deviceisiphone6plus Judge whether it is 6Plus, encountered some resolution is actually (1125, 2001) test machine, That is to say Bounds.size is (375,667), speculation is a refurbished machine

3.Deviceisipad When judging the ipad, there are more non-retina resolution ratios. Because debugging only iphone-type apps on ipad, you'll encounter this type of resolution

Macro definitions for the 4.ResizeSide and font series provide a simple reference scheme for width and font adaptation to multiple devices with code, and of course the masonry class library is the best solution for code adaptation devices, and is just for easy use

5.loadimage macro definition, uses the frequent IO way to load the picture resources, the purpose is avoids the memory cache to occupy for a long time. It is recommended that you add attributes or variable references when you need to preserve objects

6. If you are referencing a macro definition, __weak typeof (self) weakself = self; the typeof in the notation can also be replaced with the previous version __typeof, but the new notation is recommended, which is TypeOf

Focus on documenting the use of custom fonts

Previously, when configuring the Info.plist file, added a font key-value pair, which can now be used.

1. Add the. TTF font to the project first, and then in the source code of the Info.plist file, add the following code under the Uiappfonts key:

< Key > Uiappfonts</key>    <array>        <  string>fzzy.ttf</string>    </  array>

Of course, you can also find the fonts provided by Application key in list mode, and then add an item to fill in the TTF file name.

2. Use the following code in the appropriate place (for example, the agent method after the Appdelegate application is started):

 for inch [Uifont Familynames]) {        LOG (@ "familyname:%@", familyname);          for inch [Uifont Fontnamesforfamilyname:familyname]) {            LOG (@ "fontname:%@", FontName);        }    }

3. After running the app, look at the font output log in the console, since the Fzzy font is used, the actual name should be similar. So I found afont name like "fzy3jw--gb1-0."

4. You can view fontfzzyfixed macro-defined font display effect

It is important to note that in the same font family, fine, bold, italic, and so on, are different font names, which can be understood as using different fonts. A TTF font file, typically with only one font, which requires viewing the console output's font log.

Use of header files

Create a header file under the supporting files directory:

I put the generic macro definition into the COMMONHEADER.H for unified management and expansion.

Similarly, different types of macro definitions are suggested to be organized into different header files, for example: Stringsheader.h,imagesheader.h,urlsheader.h.

Finally, the integration of all header file references into the precompiled header file PrefixHeader.h the global effect.

The base project has been updated: [Email protected]:alongway/base.git

General macro definitions and header files for the app development process

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.