ios 中常用方法整理

一. 常用工具分类

1.一般项目中都有一套自己的颜色以及字体

如果所有颜色以及字体都是在每一处使用时定义,如果后期存在整体颜色与字体的调整就是一个悲剧又到处是坑的任务。所以在项目开始时就先自定义好常用字体与颜色是十分有用的
一般对字体与颜色的实用:

1
2
3
4
5
6
7
8
9
10
11
//color
[UIColor blueColor];
[UIColor colorWithRed:r / 255.0f
green:g / 255.0f
blue:b / 255.0f
alpha:alpha]

//font
[UIFont sytemFontWithSize:16];
[UIFont boldSytemFontWithSize:16];
[UIFont fontWithName:@"DIN Alternate" size:16];

对color用分类:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@interface UIColor (custom)

+ (UIColor *)colorWithRGBHex:(UInt32)hex;

+ (UIColor *)colorWithRGBHex:(UInt32)hex alpha:(CGFloat)alpha;

+ (UIColor *)gray;

+ (UIColor *)white;

+ (UIColor *)holdPlaceColor;

+ (UIColor *)borderColor;

@end

@implementation UIColor (custom)

+ (UIColor *)colorWithRGBHex:(UInt32)hex {
return [self colorWithRGBHex:hex alpha:1.0];
}

+ (UIColor *)colorWithRGBHex:(UInt32)hex alpha:(CGFloat)alpha {
int r = (hex >> 16) & 0xFF;
int g = (hex >> 8) & 0xFF;
int b = (hex) & 0xFF;

return [UIColor colorWithRed:r / 255.0f
green:g / 255.0f
blue:b / 255.0f
alpha:alpha];
}

+ (UIColor *)green {
return [self colorWithRGBHex:0x00bd9a];
}

+ (UIColor *)gray {
return [self colorWithRGBHex:0x9B9B9B];
}

+ (UIColor *)holdPlaceColor {
return [self colorWithRGBHex:0xD8D8D8];
}

+ (UIColor *)borderColor {
return [self colorWithRGBHex:0xe7ebee];
}

+ (UIColor *)white {
return [self colorWithRGBHex:0xe7ebee];
}

@end

对常用字体方法的分类,可以定义按国际化实用字体,下面列举中英文字体

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@interface UIFont (custom)

+ (UIFont *)localeFontWithSize:(CGFloat)size;

+ (UIFont *)boldLocaleFontWithSize:(CGFloat)size;

+ (UIFont *)locale12Font;

+ (UIFont *)locale13Font;

+ (UIFont *)boldLocale12Font;

+ (UIFont *)boldLocale13Font;

@end

static NSString *DinFontName = @"DIN Alternate";//英文字体,中文为系统字体
static NSString *BoldDinFontName = @"DINAlternate-Bold";

@implementation UIFont (custom)

+ (NSString *)localeLanguageCode {
return [[NSUserDefaults standardUserDefaults] stringForKey:@"appLanguage"];
}

+ (UIFont *)localeFontWithSize:(CGFloat)size {
NSString *localeLanguageCode = [self localeLanguageCode];
if ([localeLanguageCode isEqualToString:@"en"]) {
return [UIFont fontWithName:DinFontName size:size];
} else {
return [UIFont systemFontOfSize:size];
}
}

+ (UIFont *)boldLocaleFontWithSize:(CGFloat)size {
NSString *localeLanguageCode = [self localeLanguageCode];
if ([localeLanguageCode isEqualToString:@"en"]) {
return [UIFont fontWithName:BoldDinFontName size:size];
} else {
return [UIFont boldSystemFontOfSize:size];
}
}

+ (UIFont *)locale12Font {
return [self localeFontWithSize:12];
}

+ (UIFont *)locale13Font {
return [self localeFontWithSize:13];
}

+ (UIFont *)boldLocale12Font {
return [self boldLocaleFontWithSize:12];
}

+ (UIFont *)boldLocale13Font {
return [self boldLocaleFontWithSize:13];
}

@end

2. 快速的UIBUtong,UILabel生成。

一般项目中实用较多且初始化代码需要多行的View就非UIButton与UILabel了,快速生成这两个View对开发效率以及代码量的减少十分有用

一般自定一个UIButton与一个UILabel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitleColor:color forState:UIControlStateNormal];
[button setTitle:normalText forState:UIControlStateNormal];
[button setImage:image forState:UIControlStateNormal];
button.titleLabel.font = font;
button.layer.masksToBounds = YES;
button.layer.borderColor = borderColor.CGColor;
button.layer.borderWidth = borderWidth;
button.layer.cornerRadius = radius;

//label
UILabel *label = [UILabel new];
label.textColor = color;
label.font = font;
label.textAlignment = textAlign;
label.text = text;

简化操作分类:
UIButton

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
33
34
35
36
37
38
@interface UIButton (custom)

+ (instancetype)initWithFont:(UIFont *)font textColor:(UIColor *)color normalText:(NSString *)normalText;

+ (instancetype)initWithFont:(UIFont *)font textColor:(UIColor *)color normalText:(NSString *)normalText borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth;

+ (instancetype)initWithFont:(UIFont *)font textColor:(UIColor *)color normalText:(NSString *)normalText borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth redius:(CGFloat)radius;

@end

@implementation UIButton (custom)

+ (instancetype)initWithFont:(UIFont *)font textColor:(UIColor *)color normalText:(NSString *)normalText {
return [self initWithFont:font textColor:color normalText:normalText borderColor:nil borderWidth:0 redius:0];
}

+ (instancetype)initWithFont:(UIFont *)font textColor:(UIColor *)color normalText:(NSString *)normalText borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth {
return [self initWithFont:font textColor:color normalText:normalText borderColor:borderColor borderWidth:borderWidth redius:0];
}

+ (instancetype)initWithFont:(UIFont *)font textColor:(UIColor *)color normalText:(NSString *)normalText borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth redius:(CGFloat)radius {
UIButton *button = [[self class] buttonWithType:UIButtonTypeCustom];
[button setTitleColor:color forState:UIControlStateNormal];
[button setTitle:normalText forState:UIControlStateNormal];
button.titleLabel.font = font;
if (borderColor) {
button.layer.masksToBounds = YES;
button.layer.borderColor = borderColor.CGColor;
button.layer.borderWidth = borderWidth;
}
if (radius > 0) {
button.layer.cornerRadius = radius;
}
return button;
}

@end

UILabel

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
33
34
35
36
37
38
@interface UILabel (custom)

+ (instancetype)initWithFont:(UIFont *)font color:(UIColor *)color;

+ (instancetype)initWithFont:(UIFont *)font color:(UIColor *)color text:(NSString *)text;

+ (instancetype)initWithFont:(UIFont *)font color:(UIColor *)color textAlign:(NSTextAlignment)textAlign;

+ (instancetype)initWithFont:(UIFont *)font color:(UIColor *)color text:(NSString *)text textAlign:(NSTextAlignment)textAlign ;

@end


@implementation UILabel (custom)

+ (instancetype)initWithFont:(UIFont *)font color:(UIColor *)color {
return [self initWithFont:font color:color textAlign:NSTextAlignmentLeft];
}

+ (instancetype)initWithFont:(UIFont *)font color:(UIColor *)color textAlign:(NSTextAlignment)textAlign {
return [self initWithFont:font color:color text:nil textAlign:textAlign];
}

+ (instancetype)initWithFont:(UIFont *)font color:(UIColor *)color text:(NSString *)text {
return [self initWithFont:font color:color text:text textAlign:NSTextAlignmentLeft];
}

+ (instancetype)initWithFont:(UIFont *)font color:(UIColor *)color text:(NSString *)text textAlign:(NSTextAlignment)textAlign {
UILabel *label = [UILabel new];
label.textColor = color;
label.font = font;
label.textAlignment = textAlign;
if (text.length) {
label.text = text;
}
return label;
}

之后初始化的代码就可以写成

1
2
3
UIButton  *closeButton = [UIButton initWithFont:[UIFont boldLocale17Font] textColor:[UIColor whiteColor] normalText:text];

UILabel *placeLabel = [UILabel initWithFont:[UIFont locale12Font] color:[UIColor block]];

3. 常用UIImage方法

a. 按颜色生成图片

1
2
3
4
5
6
7
8
9
10
11
12
+ (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

b. 生成渐变图片

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
typedef NS_ENUM(NSUInteger, GradientDirection) {
GradientDirectionTopToBottom = 1, //从上到下
GradientDirectionLeftToRight = 2, //从左到右
GradientDirectionLeftTopToRightBottom = 3, //从左上到右下
EGradientDirectionLeftBottomToRightTop = 4 //从左下到右上
};

@interface GradientModel: NSObject

@property (nonatomic, strong) UIColor *color;
@property (nonatomic, strong) NSNumber *percent;
- (instancetype)initWithGradientColor:(UIColor *)color percent:(NSNumber *)percent;

@end

@implementation GradientModel

- (instancetype)initWithGradientColor:(UIColor *)color percent:(NSNumber *)percent {
self = [super init];
if (self) {
_color = color;
_percent = percent;
}
return self;
}

@end

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
@interface UIImage (Gradient)
/**
* 根据给定的颜色,生成渐变色的图片
* @param imageSize 要生成的图片的大小
* @param beginColor 起始颜色
* @param endColor 截点颜色
* @param gradientType 渐变色的类型
*/
+ (UIImage *)createRegularImageWithSize:(CGSize)imageSize beginColor:(UIColor *)beginColor endColor:(UIColor *)endColor gradientType:(GradientDirection)gradientType;

/**
* 根据给定的颜色,生成渐变色的图片
* @param imageSize 要生成的图片的大小
* @param gradientArray 渐变颜色的数组 和 占位置组(0 - 1)
* @param gradientType 渐变色的类型
*/
+ (UIImage *)createImageWithSize:(CGSize)imageSize gradientModelArray:(NSArray<GradientModel *> *)gradientArray gradientType:(GradientDirection)gradientType;

@end


@implementation UIImage (Gradient)

+ (UIImage *)createRegularImageWithSize:(CGSize)imageSize beginColor:(UIColor *)beginColor endColor:(UIColor *)endColor gradientType:(GradientDirection)gradientType {
NSMutableArray *gradientArray = [NSMutableArray array];
GradientModel *firstModel = [[GradientModel alloc] initWithGradientColor:beginColor percent:@(0)];
[gradientArray addObject:firstModel];
GradientModel *lastModel = [[GradientModel alloc] initWithGradientColor:endColor percent:@(1)];
[gradientArray addObject:lastModel];
return [UIImage createImageWithSize:imageSize gradientModelArray:gradientArray gradientType:gradientType];
}

+ (UIImage *)createImageWithSize:(CGSize)imageSize gradientModelArray:(NSArray<GradientModel *> *)gradientArray gradientType:(GradientDirection)gradientType {

NSMutableArray *array = [NSMutableArray array];
CGFloat locations[gradientArray.count];
for (NSInteger i = 0; i < gradientArray.count; i++) {
GradientModel *gradientModel = gradientArray[i];
[array addObject:(id)gradientModel.color.CGColor];
locations[i] = [gradientModel.percent floatValue];
}

UIGraphicsBeginImageContextWithOptions(imageSize, YES, 1);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
GradientModel *gradientModel = gradientArray.lastObject;
CGColorSpaceRef colorSpace = CGColorGetColorSpace([gradientModel.color CGColor]);
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)array, locations);
CGPoint start;
CGPoint end;
switch (gradientType) {
case GradientDirectionTopToBottom:
start = CGPointMake(imageSize.width/2, 0.0);
end = CGPointMake(imageSize.width/2, imageSize.height);
break;
case GradientDirectionLeftToRight:
start = CGPointMake(0.0, imageSize.height/2);
end = CGPointMake(imageSize.width, imageSize.height/2);
break;
case GradientDirectionLeftTopToRightBottom:
start = CGPointMake(0.0, 0.0);
end = CGPointMake(imageSize.width, imageSize.height);
break;
case GradientDirectionLeftBottomToRightTop:
start = CGPointMake(0.0, imageSize.height);
end = CGPointMake(imageSize.width, 0.0);
break;
default:
break;
}
CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGGradientRelease(gradient);
CGContextRestoreGState(context);
CGColorSpaceRelease(colorSpace);
UIGraphicsEndImageContext();
return image;
}

@end

二. 带刘海的iphone适配。

因为刘海屏实际相对于iphone 5s,iphone 6,7,8来说就是StatusBar的height不同,所以可以用

1
2
3
4
5
6
7
8
9
10
#define StatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height

#define NavigatorBarHeight 44.0

#define TabBarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height > 20 ? 83 : 49)

#define TopBarHeight (StatusBarHeight + NavigatorBarHeight)

#define BottomHeight ([[UIApplication sharedApplication] statusBarFrame].size.height > 20 ? 34 : 0)

来定义刘海屏的Navigationbar的高度,Tabbar的高度。

用StatusBarHeight > 20既可以判断是否为刘海屏(XR,XMAX及以前机型)

对屏幕高宽的常用分类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@interface UIScreen (custom)

+ (CGFloat)width;

+ (CGFloat)height;

@end

@implementation UIScreen (custom)

+ (CGFloat)width {
return [UIScreen mainScreen].bounds.size.width;
}

+ (CGFloat)height {
return [UIScreen mainScreen].bounds.size.height;
}

+ (CGFloat)screenRatio {
return screenRaitoFunc();
}

@end