首页 > 代码库 > 一些常用的宏定义(很有用,很有效噢)

一些常用的宏定义(很有用,很有效噢)

// 安全释放
#define RELEASE_SAFELY(__Pointer) do{[__Pointer release],__Pointer = nil;} while(0)

// 屏幕的物理高度
#define  ScreenHeight  [UIScreen mainScreen].bounds.size.height

// 屏幕的物理宽度
#define  ScreenWidth   [UIScreen mainScreen].bounds.size.width

// 调试
#define NSLOG_FUNCTION NSLog(@"%s,%d",__FUNCTION__,__LINE__)



//----------------------图片————————————————————————————————————————
  
//读取本地图片  
#define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]  
  
//定义UIImage对象  
#define IMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]  
  
//定义UIImage对象  
#define ImageNamed(_pointer) [UIImage imageNamed:_pointer] 



//———————————————————————颜色类———————————————————————————-----—————————————
// rgb颜色转换(16进制->10进制)  
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]  
  
//带有RGBA的颜色设置  
#define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]  
  
// 获取RGB颜色  
#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)  
  
//背景色  
#define BACKGROUND_COLOR [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]  
  
//清除背景色  
#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)]  




//———————————————————————TAG宏———————————————————————————-----—————————————
//viewWithTag
#define VIEWWITHTAG(_OBJECT, _TAG)    [_OBJECT viewWithTag : _TAG] 




//———————————————————————单例模式宏———————————————————————————-----—————————————
      //.h文件处的声明
#define DEFINE_SINGLETON_FOR_HEADER(className) + (className *)shared##className;

      //.m文件处的声明
#define DEFINE_SINGLETON_FOR_CLASS(className) + (className *)shared##className {     static className *shared##className = nil;     static dispatch_once_t onceToken;     dispatch_once(&onceToken, ^{         shared##className = [[self alloc] init];     });     return shared##className; }

//———————————————————————单例模式———————————————————————————-----—————————————
static DataBaseHandle * handle = nil;
+ (DataBaseHandle *)shareInstance
{
	@synchronized(self){
		if (!handle) {
			handle = [[DataBaseHandle alloc] init];
		}
	}
	return handle;
}

—————————————不用你把所有NSLog的删除或注释,直接修改#if 判断的值就行了(1执行,0不执行)——————————————
#if 1

#define NSLog(FORMAT, ...) fprintf(stderr,"[%s:%d行] %s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else

#define NSLog(FORMAT, ...) nil

#endif


#define NEED_OUTPUT_LOG                     0
#if NEED_OUTPUT_LOG

    #define SLog(xx, ...)   NSLog(xx, ##__VA_ARGS__)
    #define SLLog(xx, ...)  NSLog(@"%s(%d): " xx, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)

    #define SLLogRect(rect)     SLLog(@"%s x=%f, y=%f, w=%f, h=%f", #rect, rect.origin.x, rect.origin.y,     rect.size.width, rect.size.height)

    #define SLLogPoint(pt)     SLLog(@"%s x=%f, y=%f", #pt, pt.x, pt.y)

    #define SLLogSize(size)     SLLog(@"%s w=%f, h=%f", #size, size.width, size.height)

    #define SLLogColor(_COLOR)     SLLog(@"%s h=%f, s=%f, v=%f", #_COLOR, _COLOR.hue, _COLOR.saturation, _COLOR.value)

    #define SLLogSuperViews(_VIEW)     { for (UIView* view = _VIEW; view; view = view.superview) { SLLog(@"%@", view); } }

    #define SLLogSubViews(_VIEW)     { for (UIView* view in [_VIEW subviews]) { SLLog(@"%@", view); } }

#else

    #define SLog(xx, ...)  ((void)0)
    #define SLLog(xx, ...)  ((void)0)

#endif