1. 可能不定期添加新的东西
  2. github地址:https://github.com/yuqingzhude/CommonUseDemo
  3.  
  4. /************************Tools************************/
  5.  
  6. /** 存网络请求参数或者其他类似key value对应的字典*/
  7. @property (nonatomic,strong) NSDictionary *keyValueDic;
  8.  
  9. + (instancetype)sharedCommonUse;
  10.  
  11. /** 获取当前时间*/
  12. + (NSString *)getCurrentTime;
  13.  
  14. /** 将日期格式转化成字符串*/
  15. +(NSString*)getFormatedTime:(NSDate*)date;
  16.  
  17. /** 改变图片大小*/
  18. +(UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize;
  19.  
  20. /** 拨打电话*/
  21. + (void)callSomebody:(NSString *)tel;
  22.  
  23. /** 字符串是否为数字*/
  24. + (BOOL)ifThisStringIsAnNumber:(NSString *)str;
  25.  
  26. /** 字符串是否为整形*/
  27. + (BOOL)isPureInt:(NSString*)string;
  28.  
  29. /** 判断是否为浮点形*/
  30. + (BOOL)isPureFloat:(NSString*)string;
  31.  
  32. /** 从栈中将要移动vc向前移动index个位置,中间的vc将被释放*/
  33. //ex:pay success pop start vc
  34. + (void)moveForwardTopVC:(UIViewController *)vc ForwardIndex:(NSInteger)index;
  35.  
  36. /** pop到指定名字的VC,没有index可能变化的引起的问题*/
  37. + (void)popToViewControllerWithVC:(UIViewController *)vc toClass:(Class)toClass animated:(BOOL)animated;
  38.  
  39. /** 传入数据字典&Model名打印出Model Body*/
  40. +(void)createModelWithDictionary:(NSDictionary *)dict modelName:(NSString *)modelName;
  41.  
  42. /** 计算字符串高度*/
  43. - (float) calculateStrheightWithStr:(NSString *)str Font: (UIFont *)font Width: (float) width;
  44.  
  45. /** 计算字符串宽度*/
  46. - (float) calculateStrwidthWithStr:(NSString *)str Font: (UIFont *) font;
  47.  
  48. /** 将一个UIView切成圆形 宽高得相等*/
  49. + (void)changeViewToCircle:(UIView *)view;
  50.  
  51. /** collectionView 重新加载数据后计算出高度并更新*/
  52. + (void)reloadCollectionViewHeight:(UICollectionView *)collectionView;
  53.  
  54. // Factory
  55.  
  56. /** 创建btn 基本属性: frame title titleColor backgroundColor*/
  57. + (UIButton *)createButtonWithFrame:(CGRect)frame Title:(NSString *)title TitleColor:(UIColor *)titleColor BackgroundColor:(UIColor *)backgroundColor;
  58.  
  59. /** 创建btn 基本属性 + 边框 + 圆角 + 背景图*/
  60. + (UIButton *)createButtonWithFrame:(CGRect)frame Title:(NSString *)title TitleColor:(UIColor *)titleColor BackgroundColor:(UIColor *)backgroundColor CornerRadius:(CGFloat)cornerRadius BorderWidth:(CGFloat)borderWidth BorderColor:(UIColor *)borderColor ImageName:(NSString *)imageName;
  61.  
  62. /** 创建label 基本属性 frame text font textColor*/
  63. + (UILabel *)createLabelWithFrame:(CGRect)frame Text:(NSString *)text Font:(UIFont *) font TextColor:(UIColor *)textColor;
  64.  
  65. /** 创建label 基本属性 + backgroundColor + textAlinment */
  66. + (UILabel *)createLabelWithFrame:(CGRect)frame Text:(NSString *)text Font:(UIFont *) font TextColor:(UIColor *)textColor BackgroundColor:(UIColor *)backgroundColor TextAlignment:(NSTextAlignment)textAlinment;
  67.  
  68. /** 富文本单个keyword*/
  69. +(NSAttributedString *)attributeTextStr:(NSString *)text textColor:(UIColor *)textColor textFont:(UIFont *)textFont keywordStr:(NSString *)keywordStr keywordColor:(UIColor *)keywordColor keywordFont:(UIFont *)keywordFont;
  70.  
  71. /** 富文本多个keyword*/
  72. +(NSAttributedString *)attributeTextStr:(NSString *)text textColor:(UIColor *)textColor textFont:(UIFont *)textFont keywordStrArray:(NSArray *)keywordStrArray keywordColor:(UIColor *)keywordColor keywordFont:(UIFont *)keywordFont;
  73.  
  74. @end
  75.  
  76. /************************Define************************/
  77.  
  78. #ifdef DEBUG
  79. #define QZLog(fmt, ...) NSLog((@"owner:QZ %s [Line %d] \n " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
  80. #else
  81. #define QZLog(...)
  82. #endif
  83.  
  84. //判断是真机还是模拟器
  85. #if TARGET_OS_IPHONE
  86. //iPhone Device
  87. #endif
  88.  
  89. #if TARGET_IPHONE_SIMULATOR
  90. //iPhone Simulator
  91. #endif
  92.  
  93. #define SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
  94. #define IS_IOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0]
  95.  
  96. /** RGB颜色*/
  97. #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]
  98. #define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
  99.  
  100. #define DEVICE_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
  101. #define DEVICE_WIDTH ([[UIScreen mainScreen] bounds].size.width)
  102. #define SCREEN_HEIGHT DEVICE_HEIGHT
  103. #define SCREEN_WIDTH DEVICE_WIDTH
  104. #define NavigationBar_Height 44
  105.  
  106. /************************适配************************/
  107. #define IS_IPHONEUI (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  108. #define IS_IPADUI (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  109. #define IS_IPHONE4 ((int)DEVICE_HEIGHT%480==0)
  110. #define IS_IPHONE5 ((int)DEVICE_HEIGHT%568==0)
  111. #define IS_IPAD ((int)DEVICE_HEIGHT%1024==0)
  112. #define IS_IPHONE6 ((int)DEVICE_HEIGHT%667==0)
  113. #define IS_IPHONE6P ((int)DEVICE_HEIGHT%736==0)
  114. /*
  115. 4 inch 320*568
  116. 4.7 inch 375*667
  117. 5.5 inch 414*736
  118. */
  119. #define CONVERT_SCALE(x) (x)/2
  120.  
  121. #define CT_SCALE_X DEVICE_WIDTH/320.0
  122. #define CT_SCALE_Y DEVICE_HEIGHT/568.0
  123. // 5 6 比例一致,6p 放大
  124. #define CT6P_SCALE_X (IS_IPHONE6P?CT_SCALE_X:1)
  125. #define CT6P_SCALE_Y (IS_IPHONE6P?CT_SCALE_Y:1)
  126. //转化为最低兼容设备尺寸
  127. #define ConvertTo5_W(x) (CONVERT_SCALE(x)*320)/375
  128. #define ConvertTo5_H(x) (CONVERT_SCALE(x)*568)/667
  129.  
  130. #define CW(x) ConvertTo5_W(x)*CT_SCALE_X
  131. #define CH(x) ConvertTo5_H(x)*CT_SCALE_Y
  132. #define CWFor6P(x) ConvertTo5_W(x)*CT6P_SCALE_X
  133. #define CHFor6P(x) ConvertTo5_H(x)*CT6P_SCALE_Y
  134. /************************适配************************/
  135.  
  136. 工具方法的实现
  137.  
  138. - (NSDictionary*)keyValueDic
  139. {
  140. if (!_keyValueDic) {
  141. NSDictionary *keyValueDic=[[NSDictionary alloc] init];
  142. keyValueDic = @{
  143. @"key1":@"value1",
  144. @"key2":@"value2",
  145. @"key3":@"value3",
  146. };
  147. _keyValueDic = keyValueDic;
  148. }
  149. return _keyValueDic;
  150. }
  151.  
  152. +(NSString*)getCurrentTime{
  153. NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
  154. [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  155. NSString *dateTime=[formatter stringFromDate:[NSDate date]];
  156. return dateTime;
  157. }
  158.  
  159. +(NSString*)getFormatedTime:(NSDate*)date{
  160. NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
  161. [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  162. NSString *dateTime=[formatter stringFromDate:date];
  163. return dateTime;
  164.  
  165. }
  166.  
  167. +(UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize{
  168. UIGraphicsBeginImageContext(reSize);
  169. [image drawInRect:CGRectMake(, , reSize.width, reSize.height)];
  170. UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
  171. UIGraphicsEndImageContext();
  172. return reSizeImage;
  173. }
  174.  
  175. +(void)callSomebody:(NSString *)tel
  176. {
  177. NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",tel];
  178. UIWebView * callWebview = [[UIWebView alloc] init];
  179. [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
  180. [[UIApplication sharedApplication].keyWindow addSubview:callWebview];
  181. }
  182.  
  183. + (BOOL)ifThisStringIsAnNumber:(NSString *)str
  184. {
  185. if ([self isPureInt:str] || [self isPureFloat:str]) {
  186. return YES;
  187. }
  188. return NO;
  189. }
  190.  
  191. + (BOOL)isPureInt:(NSString*)string
  192. {
  193. NSScanner* scan = [NSScanner scannerWithString:string];
  194. int val;
  195. return[scan scanInt:&val] && [scan isAtEnd];
  196. }
  197.  
  198. + (BOOL)isPureFloat:(NSString*)string
  199. {
  200. NSScanner* scan = [NSScanner scannerWithString:string];
  201. float val;
  202. return[scan scanFloat:&val] && [scan isAtEnd];
  203. }
  204.  
  205. +(void)moveForwardTopVC:(UIViewController *)vc ForwardIndex:(NSInteger)index
  206. {
  207. // 直接操作nav的viewControllers栈
  208. NSMutableArray *stackAry=[NSMutableArray arrayWithArray:vc.navigationController.viewControllers];
  209.  
  210. for (NSInteger i=vc.navigationController.viewControllers.count-; i>=vc.navigationController.viewControllers.count--index; i--) {
  211. if(i<) return;
  212. [stackAry removeObjectAtIndex:i];
  213. }
  214. [stackAry addObject:vc];
  215. vc.navigationController.viewControllers=[stackAry copy];
  216. }
  217.  
  218. +(void)popToViewControllerWithVC:(UIViewController *)vc toClass:(Class)toClass animated:(BOOL)animated
  219. {
  220. for (UIViewController *controller in vc.navigationController.viewControllers) {
  221. if ([controller isKindOfClass:toClass]) {
  222. [vc.navigationController popToViewController:controller animated:animated];
  223. }
  224. }
  225. }
  226.  
  227. +(void)createModelWithDictionary:(NSDictionary *)dict modelName:(NSString *)modelName
  228. {
  229. printf("\n@interface %s :NSObject\n",modelName.UTF8String);
  230. for (NSString *key in dict) {
  231. NSString *type = ([dict[key] isKindOfClass:[NSNumber class]])?@"NSNumber":@"NSString";
  232. printf("@property (nonatomic,copy) %s *%s;\n",type.UTF8String,key.UTF8String);
  233. }
  234. printf("@end\n");
  235.  
  236. }
  237.  
  238. - (float) calculateStrheightWithStr:(NSString *)str Font: (UIFont *)font Width: (float) width
  239. {
  240. CGRect textRect = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
  241. options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
  242. attributes:@{NSFontAttributeName:font}
  243. context:nil];
  244.  
  245. return ceil(textRect.size.height);
  246. }
  247.  
  248. - (float) calculateStrwidthWithStr:(NSString *)str Font: (UIFont *) font
  249. {
  250. CGRect textRect = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, font.pointSize)
  251. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  252. attributes:@{NSFontAttributeName:font}
  253. context:nil];
  254.  
  255. return ceil(textRect.size.width);
  256. }
  257.  
  258. + (void)changeViewToCircle:(UIView *)view
  259. {
  260. view.layer.cornerRadius = view.bounds.size.width/2.0;
  261. view.clipsToBounds = YES;
  262. }
  263.  
  264. + (void)reloadCollectionViewHeight:(UICollectionView *)collectionView
  265. {
  266. CGFloat x = collectionView.frame.origin.x;
  267. CGFloat y = collectionView.frame.origin.y;
  268. CGFloat width = collectionView.bounds.size.width;
  269. CGFloat height = collectionView.collectionViewLayout.collectionViewContentSize.height;
  270. collectionView.frame = CGRectMake(x, y, width, height);
  271. }
  272.  
  273. +(NSAttributedString *)attributeTextStr:(NSString *)text textColor:(UIColor *)textColor textFont:(UIFont *)textFont keywordStr:(NSString *)keywordStr keywordColor:(UIColor *)keywordColor keywordFont:(UIFont *)keywordFont
  274. {
  275. return [CommonUse attributeTextStr:text textColor:textColor textFont:textFont keywordStrArray:keywordStr?@[keywordStr]:nil keywordColor:keywordColor keywordFont:keywordFont];
  276. }
  277.  
  278. +(NSAttributedString *)attributeTextStr:(NSString *)text textColor:(UIColor *)textColor textFont:(UIFont *)textFont keywordStrArray:(NSArray *)keywordStrArray keywordColor:(UIColor *)keywordColor keywordFont:(UIFont *)keywordFont
  279. {
  280. NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:text];
  281. [attr addAttributes:@{NSFontAttributeName:textFont?:[UIFont systemFontOfSize:],NSForegroundColorAttributeName:textColor?:[UIColor blackColor]} range:NSMakeRange(, text.length)];
  282. for (NSString *tempstr in keywordStrArray) {
  283. [attr addAttributes:@{NSFontAttributeName:keywordFont?:[UIFont systemFontOfSize:],NSForegroundColorAttributeName:keywordColor?:[UIColor blackColor]} range:[text rangeOfString:tempstr]];
  284. }
  285. return attr;
  286. }
  287.  
  288. /** Button Factory*/
  289. + (UIButton *)createButtonWithFrame:(CGRect)frame Title:(NSString *)title TitleColor:(UIColor *)titleColor BackgroundColor:(UIColor *)backgroundColor
  290. {
  291. UIButton *btn = [[UIButton alloc] init];
  292. btn.frame = frame;
  293. [btn setTitle:title forState:UIControlStateNormal];
  294. [btn setTitleColor:titleColor ? : [UIColor whiteColor] forState:UIControlStateNormal];
  295. btn.backgroundColor = backgroundColor ? :[UIColor whiteColor];
  296. return btn;
  297. }
  298.  
  299. + (UIButton *)createButtonWithFrame:(CGRect)frame Title:(NSString *)title TitleColor:(UIColor *)titleColor BackgroundColor:(UIColor *)backgroundColor CornerRadius:(CGFloat)cornerRadius BorderWidth:(CGFloat)borderWidth BorderColor:(UIColor *)borderColor ImageName:(NSString *)imageName
  300. {
  301. UIButton *btn = [CommonUse createButtonWithFrame:frame Title:title TitleColor:titleColor BackgroundColor:backgroundColor];
  302. btn.layer.cornerRadius = cornerRadius;
  303. if (borderWidth > ) {
  304. btn.layer.borderWidth=borderWidth;
  305. }
  306. if (borderColor) {
  307. btn.layer.borderColor=[borderColor CGColor];
  308. }
  309. btn.layer.masksToBounds=YES;
  310. [btn setImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
  311. return btn;
  312. }
  313.  
  314. + (UILabel *)createLabelWithFrame:(CGRect)frame Text:(NSString *)text Font:(UIFont *) font TextColor:(UIColor *)textColor
  315. {
  316. UILabel *label = [[UILabel alloc] init];
  317. label.frame = frame;
  318. label.text = text ? :@"";
  319. if (font) {
  320. label.font = font;
  321. }
  322. if (textColor) {
  323. label.textColor = textColor;
  324. }
  325. return label;
  326. }
  327.  
  328. + (UILabel *)createLabelWithFrame:(CGRect)frame Text:(NSString *)text Font:(UIFont *) font TextColor:(UIColor *)textColor BackgroundColor:(UIColor *)backgroundColor TextAlignment:(NSTextAlignment)textAlinment
  329. {
  330. UILabel *label = [CommonUse createLabelWithFrame:frame Text:text Font:font TextColor:textColor];
  331. if (backgroundColor) {
  332. label.backgroundColor = backgroundColor;
  333. }
  334. label.textAlignment = textAlinment;
  335. return label;
  336. }

IOS - 常用宏定义和功能方法的更多相关文章

  1. iOS - 常用宏定义和PCH文件知识点整理

    (一)PCH文件操作步骤演示: 第一步:图文所示: 第二步:图文所示: (二)常用宏定义整理: (1)常用Log日志宏(输出日志详细可定位某个类.某个函数.某一行) //=============== ...

  2. iOS常用宏 定义

    总结了iOS开发过程中的一些常用宏,以后会陆陆续续添加进来. 字符串是否为空 1   #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull c ...

  3. iOS常用宏定义--实用

    在这里给大家分享一些常用的宏定义,喜欢的小伙伴可以直接在项目中使用(持续更新)!为了大家使用方便,请点击GitHub - 宏定义头文件下载 ! 1.获取屏幕宽度与高度 #define SCREEN_W ...

  4. iOS常用宏定义大全

    宏定义与常量的区别 宏:只是在预处理器里进行文本替换,不做任何类型检查,宏能定义代码,const不能,多个宏编译时间相对较长,影响开发效率,调试过慢,const只会编译一次,缩短编译时间. 所以在使用 ...

  5. iOS常用宏定义

    转发:https://www.douban.com/note/486674206/ #ifndef MacroDefinition_h#define MacroDefinition_h //----- ...

  6. (转)iOS 常用宏定义

    #ifndef MacroDefinition_h #define MacroDefinition_h   //-------------------获取设备大小------------------- ...

  7. IOS 程序员开发最常用宏定义

    网上对IOS的宏定义比较多,我总结了一些最常用的宏,后续还会继续补上. 1.首次启动判断: #define First_Launched @"firstLaunch" 2.ios7 ...

  8. ios 常用宏(copy)

    分享一下我现在用的 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...

  9. iOS 使用宏定义函数和代码块

    iOS使用宏定义函数和代码块 今天在开发过程中碰到一个问题:就是父类中要向外发送通知,然后子类中或者其他类中来接收它.当然一般是把它写到类方法中去,但是有个问题,就是如果调用的类不是它的子类,就不能直 ...

随机推荐

  1. Terrocotta - 基于JVM的Java应用集群解决方案

    前言 越来越多的企业关键应用都必须采用集群技术,实现负载均衡(Load Balancing).容错(Fault Tolerance)和灾难恢复(Failover).以达到系统可用性(High Avai ...

  2. 《C#并行编程高级教程》第9章 异步编程模型 笔记

    这个章节我个人感觉意义不大,使用现有的APM(异步编程模型)和EAP(基于时间的异步模型)就很够用了,针对WPF和WinForm其实还有一些专门用于UI更新的类. 但是出于完整性,还是将一下怎么使用. ...

  3. ps一般常用的快捷键

    jpg 不支持透明和半透明 :网页中的大图,高清图(体积大) gif 支持透明 不支持半透明 :网页中的小图标(动画图片)png8 支持透明 不支持半透明 :网页中的小图标png24 支持透明 支持半 ...

  4. Ramdisk文件系统的制作与调试运行

    开发环境:Fedora 9交叉编译工具链:arm-linux-gcc 4.3.2 with EABI嵌入式Linux内核版本:2.6.29.4-FriendlyARM.昨天写贴子的时候具体记不清了,今 ...

  5. 28、activity之间传递数据&批量传递数据

    import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android ...

  6. bzoj 3198 [Sdoi2013]spring(容斥原理+Hash)

    Description Input Output Sample Input 3 3 1 2 3 4 5 6 1 2 3 0 0 0 0 0 0 4 5 6 Sample Output 2 HINT [ ...

  7. STL --最常见的容器使用要点

    如果只是要找到STL某个容器的用法, 可以参考msdn和C++ Library Reference,msdn 上面的知识点应该是最新的,C++ Library Reference虽古老但是很多经典的容 ...

  8. android NDK 实用学习(四)-类缓存

    1,为什么需要类缓存: 答:由于频繁的查找类及类成员变量需要很大的时间与空间开销,可参考如下文章: http://www.ibm.com/developerworks/cn/java/j-jni/ h ...

  9. 【AC自动机】专题总结

    刷了一星期+的ac自动机的题目 做一个总结~ 我的ac自动机是之前省选的时候看老师给的一个网页上学的 由于找不到原文 就贴个转载的地址吧 - - http://hi.baidu.com/winterl ...

  10. rsync 的安装

    Server setup 0)yum -y install xinetd vi /etc/xinetd.d/rsync and ensure following:        disable = n ...