Common macro definitions for IOS

Source: Internet
Author: User
Tags define function

=============================================

--2015/12/9

-[UPDATE]

1, judge whether it is a real machine or simulator [Note: Remember to determine whether it is the simulator, otherwise it will fail OH]

#if target_iphone_simulator//Simulator

#define Is_iphone 0

#elif target_os_iphone//Real Machine

#define Is_iphone 1

#endif

2. Wide screen height

#pragma mark-Custom macros

Consider the effect of the turn screen, according to the actual screen orientation (uideviceorientation) of the width of the high

#define SCREEN_WIDTH ([UIScreen mainscreen].bounds.size.width)

#define Screen_height ([UIScreen mainscreen].bounds.size.height)

#define Statusbar_height ([uiapplication sharedapplication].statusbarframe.size.height)

/*

Regardless of the impact of the rotary screen, only the vertical screen (uideviceorientationportrait) of the width of the height

#define Screen_width MIN ([Uiscreenmainscreen].bounds.size.width, [Uiscreenmainscreen].bounds.size.height)

#define Screen_height MAX ([Uiscreenmainscreen].bounds.size.height, [Uiscreenmainscreen].bounds.size.width)

#define Statusbar_height MIN ([Uiapplicationsharedapplication].statusbarframe.size.width, [ Uiapplicationsharedapplication].statusbarframe.size.height)

*/

The actual width of the app, removing the "status bar"

#define APPFRAME_WIDTH ([UIScreen mainscreen].applicationframe.size.width)

#define Appframe_height ([UIScreen mainscreen].applicationframe.size.height)

3, judge whether the equipment is Iphone/ipad

#define Is_iphone (ui_user_interface_idiom () = = Uiuserinterfaceidiomphone)

#define IS_IPAD (ui_user_interface_idiom () = = Uiuserinterfaceidiompad)

4. Random Color

#define Mbrandomcolor [Uicolor colorwithred:arc4random_uniform (255)/255.0 Green:arc4random_uniform (255)/255.0 Blue: Arc4random_uniform (255)/255.0 Alpha:1]

Quickly create colors

#define Mbcolor (R, G, B, a) [Uicolor colorwithred: (r/255.0) Green: (g/255.0) Blue: (b/255.0) alpha:a]

5. Custom Print Log

#ifdef DEBUG

#define MYLOG (FMT, ...) NSLog (@ "mylog:\n FUNC:%s |**| [Line%d] \ n "FMT", __func__, __line__, # #__VA_ARGS__)

#define MBLOG (FMT, ...) NSLog (@ "[Line%d]--" fmt), __line__, # #__VA_ARGS__)

#else

#define MYLOG (FMT, ...)

#define MBLOG (FMT, ...)

#endif

6. Current system version

#define CURRENT_SYSTEM_VERSION ([[[Uidevice Currentdevice] systemversion] floatvalue])

=============================================

#ifndef Macrodefinition_h
#define Macrodefinition_h

-------------------get the device size-------------------------
NavBar Height
#define Navigationbar_height 44

Get screen width, height
#define SCREEN_WIDTH ([UIScreen mainscreen].bounds.size.width)
#define Screen_height ([UIScreen mainscreen].bounds.size.height)

-------------------get the device size-------------------------


-------------------Print Log-------------------------
Print log in DEBUG mode, when moving forward
#ifdef DEBUG
# define DLOG (FMT, ...) NSLog (@ "%s [line%d]" FMT), __pretty_function__, __line__, # #__VA_ARGS__);
#else
# define DLOG (...)
#endif


To rewrite the print log and the current number of lines in Nslog,debug mode
#if DEBUG
#define NSLOG (FORMAT, ...) fprintf (stderr, "\nfunction:%s line:%d content:%s\n", __function__, __line__, [NSString Stringwithformat:format, # #__VA_ARGS__] utf8string]);
#else
#define NSLOG (FORMAT, ...) nil
#endif

Print log in DEBUG mode, current line and pop up a warning
#ifdef DEBUG
# define Ulog (FMT, ...) {Uialertview *alert = [Uialertview alloc] initwithtitle:[nsstring stringwithformat:@ "%s\n [line%d]", __pretty_function __, __line__] message:[nsstring stringwithformat:fmt, # #__VA_ARGS__] delegate:nil cancelbuttontitle:@ "OK" Otherbuttontitles:nil]; [Alert show]; }
#else
# define Ulog (...)
#endif


#define Ittdebug
#define ITTLOGLEVEL_INFO 10
#define Ittloglevel_warning 3
#define ITTLOGLEVEL_ERROR 1

#ifndef Ittmaxloglevel

#ifdef DEBUG
#define Ittmaxloglevel Ittloglevel_info
#else
#define Ittmaxloglevel Ittloglevel_error
#endif

#endif

The general purpose logger. This ignores logging levels.
#ifdef Ittdebug
#define Ittdprint (XX, ...) NSLog (@ "%s (%d):" XX, __pretty_function__, __line__, # #__VA_ARGS__)
#else
#define Ittdprint (XX, ...) ((void) 0)
#endif

Print the name of the current method
#define ITTDPRINTMETHODNAME () Ittdprint (@ "%s", __pretty_function__)

Log-level based logging macros.
#if ittloglevel_error <= Ittmaxloglevel
#define ITTDERROR (XX, ...) Ittdprint (XX, # #__VA_ARGS__)
#else
#define ITTDERROR (XX, ...) ((void) 0)
#endif

#if ittloglevel_warning <= Ittmaxloglevel
#define Ittdwarning (XX, ...) Ittdprint (XX, # #__VA_ARGS__)
#else
#define Ittdwarning (XX, ...) ((void) 0)
#endif

#if ittloglevel_info <= Ittmaxloglevel
#define ITTDINFO (XX, ...) Ittdprint (XX, # #__VA_ARGS__)
#else
#define ITTDINFO (XX, ...) ((void) 0)
#endif

#ifdef Ittdebug
#define ITTDCONDITIONLOG (condition, XX, ...) {if ((condition)) {\
Ittdprint (XX, # #__VA_ARGS__); \
} \
} ((void) 0)
#else
#define ITTDCONDITIONLOG (condition, XX, ...) ((void) 0)
#endif

#define ITTASSERT (condition, ...) \
do {\
if (! ( condition)) {\
[Nsassertionhandler Currenthandler] \
Handlefailureinfunction:[nsstring stringwithutf8string:__pretty_function__] \
File:[nsstring stringwithutf8string:__file__] \
LINENUMBER:__LINE__ \
DESCRIPTION:__VA_ARGS__]; \
} \
} while (0)

---------------------Print Log--------------------------


----------------------System----------------------------

whether ipad
#define ISPAD (ui_user_interface_idiom () = = Uiuserinterfaceidiompad)
whether ipad
#define SomeThing (ui_user_interface_idiom () = = Uiuserinterfaceidiompad)? Ipad:iphone

Get System version
#define Ios_version [[Uidevice Currentdevice] systemversion] floatvalue]
#define Currentsystemversion [Uidevice Currentdevice] systemversion]

Get the current language
#define CURRENTLANGUAGE ([Nslocale preferredlanguages] objectatindex:0])

Determine if the retina screen, whether the device is%fhone 5, is an ipad
#define Isretina ([UIScreen instancesrespondtoselector: @selector (currentmode)]? Cgsizeequaltosize (Cgsizemake (640, 960), [UIScreen Mainscreen] currentmode].size): NO)
#define IPHONE5 ([UIScreen instancesrespondtoselector: @selector (currentmode)]? Cgsizeequaltosize (Cgsizemake (640, 1136), [UIScreen Mainscreen] currentmode].size): NO)
#define ISPAD (ui_user_interface_idiom () = = Uiuserinterfaceidiompad)

Judging the device's operating system is not iOS7
#define IOS7 ([[Uidevice currentdevice].systemversion Doublevalue] >= 7.0]

Determine if the current device is iphone5
#define KSCREENIPHONE5 (([UIScreen mainscreen] bounds].size.height) >=568)

Gets the height of the current screen
#define Kmainscreenheight ([UIScreen mainscreen].applicationframe.size.height)

Gets the width of the current screen
#define KMAINSCREENWIDTH ([UIScreen mainscreen].applicationframe.size.width)



Define a define function
#define Tt_release_cf_safely (__ref) {if (Nil! = (__ref)) {cfrelease (__ref); __ref = nil;}}

Judging whether it's a real machine or a simulator
#if Target_os_iphone
IPhone Device
#endif

#if Target_iphone_simulator
IPhone Simulator
#endif

Check the system version
#define SYSTEM_VERSION_EQUAL_TO (v) ([[Uidevice Currentdevice] systemversion] compare:v options:nsnumericsearch] = = Nsorderedsame)
#define System_version_greater_than (v) ([[Uidevice Currentdevice] systemversion] compare:v Options:nsnumericsearch] = = nsordereddescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO (v) ([[Uidevice Currentdevice] systemversion] compare:v options: Nsnumericsearch]! = nsorderedascending)
#define System_version_less_than (v) ([[Uidevice Currentdevice] systemversion] compare:v options:nsnumericsearch] = = nsorderedascending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO (v) ([[Uidevice Currentdevice] systemversion] compare:v options: Nsnumericsearch]! = nsordereddescending)


----------------------System----------------------------


----------------------Memory----------------------------

Use arc and do not use arc
#if __has_feature (OBJC_ARC)
Compiling with ARC
#else
Compiling without ARC
#endif

#pragma mark-common functions
#define Release_safely (__pointer) {[__pointer RELEASE]; __pointer = nil;}

Releasing an Object
#define SAFE_DELETE (P) if (p) {[P release], p = nil;}

#define SAFE_RELEASE (x) [x Release];x=nil



----------------------Memory----------------------------


----------------------Pictures----------------------------

Reading local Pictures
#define LoadImage (File,ext) [UIImage imagewithcontentsoffile:[nsbundle mainbundle]pathforresource:file OfType:ext]

Defining UIImage Objects
#define IMAGE (A) [UIImage imagewithcontentsoffile:[nsbundle Mainbundle] pathforresource:a Oftype:nil]

Defining UIImage Objects
#define IMAGENAMED (_pointer) [UIImage imagenamed:[uiutil Imagename:_pointer]

The first two macro definitions are recommended, with higher performance than the latter
----------------------Pictures----------------------------



----------------------Color Class---------------------------
RGB color conversion (16 binary->10)
#define UICOLORFROMRGB (rgbvalue) [Uicolor colorwithred: ((float) ((Rgbvalue & 0xFF0000) >>))/255.0 Green: (( float) ((Rgbvalue & 0xff00) >> 8)/255.0 Blue: ((float) (Rgbvalue & 0xFF))/255.0 alpha:1.0]

Color settings with RGBA
#define COLOR (R, G, B, A) [Uicolor colorwithred:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]

Get RGB color
#define RGBA (r,g,b,a) [Uicolor colorwithred:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define RGB (r,g,b) RGBA (r,g,b,1.0f)

Background color
#define Background_color [Uicolor colorwithred:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]

Clear background color
#define Clearcolor [Uicolor Clearcolor]

#pragma mark-color functions
#define RGBCOLOR (r,g,b) [Uicolor colorwithred: (r)/255.0f Green: (g)/255.0f Blue: (b)/255.0f Alpha:1]
#define RGBACOLOR (r,g,b,a) [Uicolor colorwithred: (r)/255.0f Green: (g)/255.0f Blue: (b)/255.0f Alpha: (a)]

----------------------Color Class--------------------------



----------------------Other----------------------------

Definition of founder's bold and simplified body
#define FONT (F) [Uifont fontwithname:@ "fzhtjw--gb1-0" size:f]


Define an API
#define APIURL @ "http://xxxxx/"
Login API
#define Apilogin [Apiurl stringbyappendingstring:@ "Login"]

Set the Tag property of the View
#define VIEWWITHTAG (_object, _tag) [_object viewwithtag: _tag]
Localization of programs, referencing internationalized files
#define MYLOCAL (x, ...) Nslocalizedstring (x, nil)

G-c-d
#define BACK (block) Dispatch_async (dispatch_get_global_queue (dispatch_queue_priority_default, 0), block)
#define MAIN (Block) Dispatch_async (Dispatch_get_main_queue (), block)

Nsuserdefaults instantiation
#define User_default [Nsuserdefaults Standarduserdefaults]


Get radians from angle get angle
#define Degreestoradian (x) (M_PI * (x)/180.0)
#define Radiantodegrees (Radian) (radian*180.0)/(M_PI)



Singleton a class
#define SYNTHESIZE_SINGLETON_FOR_CLASS (classname) \
\
static classname *shared# #classname = nil; \
\
+ (classname *) shared# #classname \
{ \
@synchronized (self) \
{ \
if (shared# #classname = = nil) \
{ \
shared# #classname = [self alloc] init]; \
} \
} \
\
return shared# #classname; \
} \
\
+ (ID) Allocwithzone: (nszone *) zone \
{ \
@synchronized (self) \
{ \
if (shared# #classname = = nil) \
{ \
shared# #classname = [Super Allocwithzone:zone]; \
return shared# #classname; \
} \
} \
\
return nil; \
} \
\
-(ID) Copywithzone: (nszone *) zone \
{ \
return self; \
}

Common macro definitions for IOS

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.