-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIFont+AdjustSize.m
151 lines (124 loc) · 4.81 KB
/
UIFont+AdjustSize.m
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// UIFont+AdjustSize.m
// Buddy
//
// Created by 胡烽 on 2018/4/10.
// Copyright © 2018年 person. All rights reserved.
//
#import "UIFont+AdjustSize.h"
#import <objc/runtime.h>
#import <objc/message.h>
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define DEFAULT_WIDTH 375.0f
#define SIZE_SCALE SCREEN_WIDTH / DEFAULT_WIDTH
#define IS_IPHONE_4 ([[UIScreen mainScreen] bounds].size.height == 480.0f)
#define IS_IPHONE_5 ([[UIScreen mainScreen] bounds].size.height == 568.0f)
#define IS_IPHONE_6 ([[UIScreen mainScreen] bounds].size.height == 667.0f)
#define IS_IPHONE_6_PLUS ([[UIScreen mainScreen] bounds].size.height == 736.0f)
#define IPHONE5_INCREMENT 2.0f // iphone 5的缩放比
#define IPHONE6P_INCREMENT 1.0f // iPhone 6p的缩放比
#pragma mark - UIFont
@implementation UIFont (AdjustSize)
+ (void)load {
Method newMethod = class_getClassMethod([self class], @selector(swizz_systemFontOfSize:));
Method method = class_getClassMethod([self class], @selector(systemFontOfSize:));
method_exchangeImplementations(newMethod, method);
Method nBoldMethod = class_getClassMethod([self class], @selector(swizz_boldSystemFontOfSize:));
Method nMethod = class_getClassMethod([self class], @selector(boldSystemFontOfSize:));
method_exchangeImplementations(nBoldMethod, nMethod);
Method nameMethod = class_getClassMethod([self class], @selector(fontWithName:size:));
Method myNameMethod = class_getClassMethod([self class], @selector(swizz_fontWithName:size:));
method_exchangeImplementations(nameMethod, myNameMethod);
}
+ (UIFont *)swizz_systemFontOfSize:(CGFloat)fontSize{
UIFont *newFont = nil;
newFont = [UIFont swizz_systemFontOfSize:fontSize * SIZE_SCALE];
return newFont;
}
+ (UIFont *)swizz_boldSystemFontOfSize:(CGFloat) fontSize{
UIFont *nBoldFont = nil;
nBoldFont = [UIFont swizz_boldSystemFontOfSize:fontSize * SIZE_SCALE];
return nBoldFont;
}
+ (UIFont *)swizz_fontWithName:(NSString *)fontName size: (CGFloat)fontSize {
UIFont *newFont = nil;
newFont = [UIFont swizz_fontWithName:fontName size:fontSize * SIZE_SCALE];
return newFont;
}
@end
#pragma mark - UIButton
@implementation UIButton (AdjustSize)
+ (void)load {
Method method = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method myMethod = class_getInstanceMethod([self class], @selector(swizz_initWithCoder:));
method_exchangeImplementations(method, myMethod);
}
- (instancetype)swizz_initWithCoder:(NSCoder *)aDecode {
[self swizz_initWithCoder:aDecode];
if (self) {
if (self.tag != -1) {
CGFloat fontSize = self.titleLabel.font.pointSize;
NSString *fontName = self.titleLabel.font.fontName;
self.titleLabel.font = [UIFont fontWithName:fontName size:fontSize];
}
}
return self;
}
@end
#pragma mark - UILabel
@implementation UILabel (AdjustSize)
+ (void)load {
Method method = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method myMethod = class_getInstanceMethod([self class], @selector(swizz_initWithCoder:));
method_exchangeImplementations(method, myMethod);
}
- (instancetype)swizz_initWithCoder:(NSCoder *)aDecode {
[self swizz_initWithCoder:aDecode];
if (self) {
if (self.tag != -1) {
CGFloat fontSize = self.font.pointSize;
NSString *fontName = self.font.fontName;
self.font = [UIFont fontWithName:fontName size:fontSize];
}
}
return self;
}
@end
#pragma mark - UITextField
@implementation UITextField (AdjustSize)
+ (void)load {
Method method = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method myMethod = class_getInstanceMethod([self class], @selector(swizz_initWithCoder:));
method_exchangeImplementations(method, myMethod);
}
- (instancetype)swizz_initWithCoder:(NSCoder *)aDecode {
[self swizz_initWithCoder:aDecode];
if (self) {
if (self.tag != -1) {
CGFloat fontSize = self.font.pointSize;
NSString *fontName = self.font.fontName;
self.font = [UIFont fontWithName:fontName size:fontSize];
}
}
return self;
}
@end
#pragma mark - TextView
@implementation UITextView (AdjustSize)
+ (void)load {
Method method = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method myMethod = class_getInstanceMethod([self class], @selector(swizz_initWithCoder:));
method_exchangeImplementations(method, myMethod);
}
- (instancetype)swizz_initWithCoder:(NSCoder *)aDecode {
[self swizz_initWithCoder:aDecode];
if (self) {
if (self.tag != -1) {
CGFloat fontSize = self.font.pointSize;
NSString *fontName = self.font.fontName;
self.font = [UIFont fontWithName:fontName size:fontSize];
}
}
return self;
}
@end