<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">
1
//
2
//
WxHxD.h
3
//
4
//
Created by YouXianMing on 14/10/29.
5
//
Copyright (c) 2014年 YouXianMing. All rights reserved.
6
//
7
8
//
寬度
9
define
UIScreenWidth [UIScreen mainScreen].bounds.size.width
10
11
//
高度
12
define
UIScreenHeight [UIScreen mainScreen].bounds.size.height
13
14
//
狀態(tài)欄高度
15
define
StatusBarHeight 20.f
16
17
//
導(dǎo)航欄高度
18
define
NavigationBarHeight 44.f
19
20
//
標(biāo)簽欄高度
21
define
TabbarHeight 49.f
22
23
//
狀態(tài)欄高度 + 導(dǎo)航欄高度
24
define
StatusBarAndNavigationBarHeight (20.f + 44.f)
25
26
define
iPhone4_4s (Width == 320.f && Height == 480.f ? YES : NO)
27
define
iPhone5_5s (Width == 320.f && Height == 568.f ? YES : NO)
28
define
iPhone6 (Width == 375.f && Height == 667.f ? YES : NO)
29
define
iPhone6_plus (Width == 414.f && Height == 736.f ? YES : NO)
//
? YES : NO 這么寫增加可讀性
</pre>
[[圖片上傳中...(image-6c4f3-1509751967770-1)]](javascript:void(0); "復(fù)制代碼")
改進(jìn):
[[圖片上傳中...(image-3265d6-1509751967770-0)]](javascript:void(0); "復(fù)制代碼")
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">
1
define
isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
2
3
define
iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
4
5
define
iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
6
7
define
iPhone6p ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(828, 1472), [[UIScreen mainScreen] currentMode].size) : NO)</pre>
1. 前言
整理和收集了IOS項目開發(fā)常用的工具類,最后也給出了源碼下載鏈接。
這些可復(fù)用的工具,一定會給你實際項目開發(fā)工作錦上添花,會給你帶來大大的工作效率。
重復(fù)造輪子的事情,除卻自我多練習(xí)編碼之外,就不要傻傻的重復(fù)造輪子了,
還是提高工作效率,早點完成工作早點回家陪老婆孩子。
2.插件目錄列表:
1、UIImage+RenderMode.h
2、UIColor+Hex.h
3、UIView+AdjustFrame.h(補(bǔ)充:Adjust是"調(diào)整"的意思)
4、宏定義定義app開發(fā)常用尺寸,例如屏幕寬高,iPhone4的尺寸等等
5、關(guān)于處理顏色比較好的工具類,功能:NSCache緩存處理顏色對象提高性能、十六進(jìn)制顏色值處理(包含了UIColor+Hex.h的功能)等等
6、關(guān)于可以修改系統(tǒng)的導(dǎo)航控制器導(dǎo)航條顏色和透明度的工具類別
7、NSTimer+Addition 關(guān)于計時器的類別工具
8、給任意的UIView添加點擊事件
IOS工具類源碼github下載地址:https://github.com/HeYang123456789/Objective-C-Tools
2.1讓圖片不要渲染的工具類
簡介:

使用代碼實例:

2.2提供十六進(jìn)制轉(zhuǎn)為色值的方法
開發(fā)背景:
項目實際開發(fā)中,美工提供給我們的顏色值可能是#ffffff十六進(jìn)制或者是OC上的表示形式0Xffffff,而不是直接的RGB色值,
但是UIKit沒有提供直接處理這兩種情況的顏色值的方法,所以就需要我們自己對UIColor進(jìn)行類別拓展方法:

代碼:
//
2 // UIColor+Hex.h
3 // Hello
4 //
5 // Created by HEYANG on 16/1/20.
6 // Copyright ? 2016年 HEYANG. All rights reserved.
7 //
8
9 #import <UIKit/UIKit.h>
10
11 @interface UIColor (Hex)
12
13 /** 默認(rèn)alpha為1 */
14 + (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue;
15
16 /** 從十六進(jìn)制字符串獲取顏色,默認(rèn)alpha為1 */
17 + (UIColor *)colorWithHexString:(NSString *)color;
18
19 /** 從十六進(jìn)制字符串獲取顏色,alpha需要自己傳遞 color:支持@“#123456”、 @“0X123456”、 @“123456”三種格式 */
20 + (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha;
21
22
23 @end
24
25 =================================================
26
27 //
28 // UIColor+Hex.m
29 // Hello
30 //
31 // Created by HEYANG on 16/1/20.
32 // Copyright ? 2016年 HEYANG. All rights reserved.
33 //
34
35 #import "UIColor+Hex.h"
36
37 @implementation UIColor (Hex)
38
39 + (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue{
40 return [UIColor colorWithRed:red green:green blue:blue alpha:1];
41 }
42
43 + (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
44 {
45 //刪除字符串中的空格
46 NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
47 // String should be 6 or 8 characters
48 if ([cString length] < 6)
49 {
50 return [UIColor clearColor];
51 }
52 // strip 0X if it appears
53 //如果是0x開頭的,那么截取字符串,字符串從索引為2的位置開始,一直到末尾
54 if ([cString hasPrefix:@"0X"])
55 {
56 cString = [cString substringFromIndex:2];
57 }
58 //如果是#開頭的,那么截取字符串,字符串從索引為1的位置開始,一直到末尾
59 if ([cString hasPrefix:@"#"])
60 {
61 cString = [cString substringFromIndex:1];
62 }
63 if ([cString length] != 6)
64 {
65 return [UIColor clearColor];
66 }
67
68 // Separate into r, g, b substrings
69 NSRange range;
70 range.location = 0;
71 range.length = 2;
72 //r 截取的range = (0,2)
73 NSString *rString = [cString substringWithRange:range];
74 //g
75 range.location = 2;// 截取的range = (2,2)
76 NSString *gString = [cString substringWithRange:range];
77 //b
78 range.location = 4;// 截取的range = (4,2)
79 NSString *bString = [cString substringWithRange:range];
80
81 // Scan values
82 unsigned int r, g, b;//將字符串十六進(jìn)制兩位數(shù)字轉(zhuǎn)為十進(jìn)制整數(shù)
83 [[NSScanner scannerWithString:rString] scanHexInt:&r];
84 [[NSScanner scannerWithString:gString] scanHexInt:&g];
85 [[NSScanner scannerWithString:bString] scanHexInt:&b];
86 return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
87 }
88
89 //默認(rèn)alpha值為1
90 + (UIColor *)colorWithHexString:(NSString *)color
91 {
92 return [self colorWithHexString:color alpha:1.0f];
93 }
94
95 @end
2.3直接調(diào)整UIView的Frame的x,y,width,height,origin,size值
簡介:關(guān)于UIView的Frame的x,y,width,height,origin,size值,在Objective-C中是無法直接賦值修改(在Swift中是可以直接賦值修改的),只能間接賦值修改,所以就需要將這部分抽離成
一個可復(fù)用的分類。補(bǔ)充:為了避免以后重復(fù)利用的過程中,方法名會和項目中其他代碼方法名沖突,所以在屬性和方法前面加了語義明確的前綴"adjust_",英文adjust就是調(diào)整的意思。
源碼:
//
2 // UIView+AdjustFrame.h Adjust:調(diào)整
3 // Hello
4 //
5 // Created by HEYANG on 16/1/20.
6 // Copyright ? 2016年 HEYANG. All rights reserved.
7 //
8
9 #import <UIKit/UIKit.h>
10
11 @interface UIView (AdjustFrame)
12
13 //類別可以拓展屬性,但是不能生成set和get方法
14 @property (assign, nonatomic) CGFloat adjust_x;
15 @property (assign, nonatomic) CGFloat adjust_y;
16 @property (assign, nonatomic) CGFloat adjust_width;
17 @property (assign, nonatomic) CGFloat adjust_height;
18 @property (assign, nonatomic) CGSize adjust_size;
19 @property (assign, nonatomic) CGPoint adjust_origin;
20
21 @end
22
23 =================================================
24
25 //
26 // UIView+AdjustFrame.m Adjust:調(diào)整
27 // Hello
28 //
29 // Created by HEYANG on 16/1/20.
30 // Copyright ? 2016年 HEYANG. All rights reserved.
31 //
32
33 #import "UIView+AdjustFrame.h"
34
35 @implementation UIView (AdjustFrame)
36
37 #pragma mark - adjust_x
38 -(void)setAdjust_x:(CGFloat)adjust_x{
39 CGRect frame = self.frame;
40 frame.origin.x = adjust_x;
41 self.frame = frame;
42 }
43
44 -(CGFloat)adjust_x{
45 return self.frame.origin.x;
46 }
47
48 #pragma mark - adjust_y
49 -(void)setAdjust_y:(CGFloat)adjust_y{
50 CGRect frame = self.frame;
51 frame.origin.y = adjust_y;
52 self.frame = frame;
53 }
54
55 - (CGFloat)adjust_y
56 {
57 return self.frame.origin.y;
58 }
59
60 #pragma mark - adjust_width
61 -(void)setAdjust_width:(CGFloat)adjust_width{
62 CGRect frame = self.frame;
63 frame.size.width = adjust_width;
64 self.frame = frame;
65 }
66 - (CGFloat)adjust_width
67 {
68 return self.frame.size.width;
69 }
70
71 #pragma mark - adjust_height
72 -(void)setAdjust_height:(CGFloat)adjust_height{
73 CGRect frame = self.frame;
74 frame.size.height = adjust_height;
75 self.frame = frame;
76 }
77 - (CGFloat)adjust_height
78 {
79 return self.frame.size.height;
80 }
81
82 #pragma mark - adjust_size
83 -(void)setAdjust_size:(CGSize)adjust_size{
84 CGRect frame = self.frame;
85 frame.size = adjust_size;
86 self.frame = frame;
87 }
88 - (CGSize)adjust_size
89 {
90 return self.frame.size;
91 }
92
93 #pragma mark - adjust_origin
94 -(void)setAdjust_origin:(CGPoint)adjust_origin{
95 CGRect frame = self.frame;
96 frame.origin = adjust_origin;
97 self.frame = frame;
98 }
99 - (CGPoint)adjust_origin
100 {
101 return self.frame.origin;
102 }
103
104 @end
2.4.宏定義app開發(fā)常用的尺寸,例如屏幕寬高,iphone4的等等

宏定義的源碼:
1 //
2 // WxHxD.h
3 //
4 // Created by YouXianMing on 14/10/29.
5 // Copyright (c) 2014年 YouXianMing. All rights reserved.
6 //
7
8 // 寬度
9 #define UIScreenWidth [UIScreen mainScreen].bounds.size.width
10
11 // 高度
12 #define UIScreenHeight [UIScreen mainScreen].bounds.size.height
13
14 // 狀態(tài)欄高度
15 #define StatusBarHeight 20.f
16
17 // 導(dǎo)航欄高度
18 #define NavigationBarHeight 44.f
19
20 // 標(biāo)簽欄高度
21 #define TabbarHeight 49.f
22
23 // 狀態(tài)欄高度 + 導(dǎo)航欄高度
24 #define StatusBarAndNavigationBarHeight (20.f + 44.f)
25
26 #define iPhone4_4s (Width == 320.f && Height == 480.f ? YES : NO)
27 #define iPhone5_5s (Width == 320.f && Height == 568.f ? YES : NO)
28 #define iPhone6 (Width == 375.f && Height == 667.f ? YES : NO)
29 #define iPhone6_plus (Width == 414.f && Height == 736.f ? YES : NO)//? YES : NO 這么寫增加可讀性
2.5處理顏色比較好的工具類。功能:NSCache緩存處理顏色對象提高性能、十六進(jìn)制顏色值處理(包含了UIColor+Hex.h的功能)等等
github源碼:https://github.com/HeYang123456789/Tools-Test-Project/tree/master/ColorStringTool/ColorStringTool/ColorStringTool
使用實例:
1 #import "ViewController.h"
2 #import "ColorTools.h"
3
4
5 @interface ViewController ()
6
7 @property (weak, nonatomic) IBOutlet UIView *colorView;
8 @property (weak, nonatomic) IBOutlet UIView *colorView2;
9
10 @end
11
12 @implementation ViewController
13
14 - (void)viewDidLoad {
15
16 //通過ColorTools內(nèi)部的webColor的key獲取顏色值,并會放入緩存中
17 self.colorView.layer.borderColor = [UIColor colorWithHexString:@"black"].CGColor;
18 self.colorView.layer.borderWidth = 2.0f;
19 self.colorView.layer.cornerRadius = 4.0f;
20
21 //通過UIColor的類方法獲取UIColor對象
22 NSString* colorStr = @"0xff0fff";
23 UIColor* color = [UIColor colorWithHexString:colorStr];
24 self.colorView.backgroundColor = color;
25
26 //通過@"0xff0fff"本身獲取UIColor對象
27 UIColor* color2 = [@"#ae2388ff" getColorFromColorhexa];
28 self.colorView2.backgroundColor = color2;
29
30 //宏定義的debug模式的打印兩個顏色對象,看看是不是具體實例
31 LogColor(color)
32 LogColor(color2)
33 }
34
35 @end
2.6修改系統(tǒng)的導(dǎo)航欄控制器的導(dǎo)航欄顏色和透明度的工具類別:
github網(wǎng)址:https://github.com/HeYang123456789/NavigationBarResetBackgroundDemo