当前位置:   article > 正文

iOS 常用宏定义Macros_ios macros

ios macros

新建一个.h文件,把自动生成的代码删掉,把下面的代码复制进去,导入头文件到pch,就可以用了

  1. //
  2. // JJMacros.h
  3. // test2.7
  4. //
  5. // Created by muzhong on 2017/3/14.
  6. // Copyright © 2017年 muzhong. All rights reserved.
  7. //
  8. #import <mach/mach_time.h> // for mach_absolute_time() and friends
  9. #import <CoreGraphics/CGBase.h>
  10. #pragma mark -
  11. #pragma mark Syntactic sugar
  12. #define NOT_ANIMATED NO
  13. #define ANIMATED YES
  14. #pragma mark -
  15. #pragma mark UIColor
  16. // example usage: UIColorFromHex(0x9daa76)
  17. #define UIColorFromHexWithAlpha(hexValue,a) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue & 0xFF00) >> 8))/255.0 blue:((float)(hexValue & 0xFF))/255.0 alpha:a]
  18. #define UIColorFromHex(hexValue) UIColorFromHexWithAlpha(hexValue,1.0)
  19. #define UIColorFromRGBA(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
  20. #define UIColorFromRGB(r,g,b) UIColorFromRGBA(r,g,b,1.0)
  21. #define kMainColor [UIColor colorWithRed:(10)/255.0 green:(166)/255.0 blue:(246)/255.0 alpha:1.0]
  22. #define kBgColor [UIColor colorWithRed:(239)/255.0 green:(239)/255.0 blue:(239)/255.0 alpha:1.0]
  23. #pragma mark -
  24. #pragma mark Logging
  25. #define LOG(fmt, ...) NSLog(@"%s: " fmt, __PRETTY_FUNCTION__, ## __VA_ARGS__)
  26. #ifdef DEBUG
  27. #define INFO(fmt, ...) LOG(fmt, ## __VA_ARGS__)
  28. #else
  29. // do nothing
  30. #define INFO(fmt, ...)
  31. #endif
  32. #define ERROR(fmt, ...) LOG(fmt, ## __VA_ARGS__)
  33. #define TRACE(fmt, ...) LOG(fmt, ## __VA_ARGS__)
  34. #define METHOD_NOT_IMPLEMENTED() NSAssert(NO, @"You must override %@ in a subclass", NSStringFromSelector(_cmd))
  35. #pragma mark -
  36. #pragma mark NSNumber
  37. #define NUM_INT(int) [NSNumber numberWithInt:int]
  38. #define NUM_FLOAT(float) [NSNumber numberWithFloat:float]
  39. #define NUM_BOOL(bool) [NSNumber numberWithBool:bool]
  40. #pragma mark -
  41. #pragma mark Frame Geometry
  42. #define CENTER_VERTICALLY(parent,child) floor((parent.frame.size.height - child.frame.size.height) / 2)
  43. #define CENTER_HORIZONTALLY(parent,child) floor((parent.frame.size.width - child.frame.size.width) / 2)
  44. // example: [[UIView alloc] initWithFrame:(CGRect){CENTER_IN_PARENT(parentView,500,500),CGSizeMake(500,500)}];
  45. #define CENTER_IN_PARENT(parent,childWidth,childHeight) CGPointMake(floor((parent.frame.size.width - childWidth) / 2),floor((parent.frame.size.height - childHeight) / 2))
  46. #define CENTER_IN_PARENT_X(parent,childWidth) floor((parent.frame.size.width - childWidth) / 2)
  47. #define CENTER_IN_PARENT_Y(parent,childHeight) floor((parent.frame.size.height - childHeight) / 2)
  48. #define WIDTH(view) view.frame.size.width
  49. #define HEIGHT(view) view.frame.size.height
  50. #define X(view) view.frame.origin.x
  51. #define Y(view) view.frame.origin.y
  52. #define LEFT(view) view.frame.origin.x
  53. #define TOP(view) view.frame.origin.y
  54. #define BOTTOM(view) (view.frame.origin.y + view.frame.size.height)
  55. #define RIGHT(view) (view.frame.origin.x + view.frame.size.width)
  56. #pragma mark -
  57. #pragma mark Screen size
  58. #define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
  59. #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
  60. //以6为标准
  61. #define adoptValue(a) (a*(SCREEN_WIDTH/375.0))
  62. //导航栏高度
  63. #define kNavBarHeight 64
  64. #pragma mark -
  65. #pragma mark other
  66. // 弱引用相关
  67. #ifndef weakify
  68. #if DEBUG
  69. #if __has_feature(objc_arc)
  70. #define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
  71. #else
  72. #define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
  73. #endif
  74. #else
  75. #if __has_feature(objc_arc)
  76. #define weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
  77. #else
  78. #define weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
  79. #endif
  80. #endif
  81. #endif
  82. #ifndef strongify
  83. #if DEBUG
  84. #if __has_feature(objc_arc)
  85. #define strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
  86. #else
  87. #define strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;
  88. #endif
  89. #else
  90. #if __has_feature(objc_arc)
  91. #define strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;
  92. #else
  93. #define strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;
  94. #endif
  95. #endif
  96. #endif
  97. //当前window
  98. #define kCurrentWindow [[UIApplication sharedApplication].windows firstObject]
  99. //非空的字符串 避免输出null
  100. #define kUnNilStr(str) ((str && ![str isEqual:[NSNull null]])?str:@"")
  101. //app名称
  102. #define kAppName [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]
  103. //app版本
  104. #define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
  105. #pragma mark -
  106. #pragma mark Device type.
  107. // Corresponds to "Targeted device family" in project settings
  108. // Universal apps will return true for whichever device they're on.
  109. // iPhone apps will return true for iPhone even if run on iPad.
  110. #define TARGETED_DEVICE_IS_IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
  111. #define TARGETED_DEVICE_IS_IPHONE UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone
  112. #define TARGETED_DEVICE_IS_IPHONE_568 TARGETED_DEVICE_IS_IPHONE && SCREEN_HEIGHT == 568
  113. #pragma mark -
  114. #pragma mark Transforms
  115. #define DEGREES_TO_RADIANS(degrees) degrees * M_PI / 180


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/223745
推荐阅读
相关标签
  

闽ICP备14008679号