-
Notifications
You must be signed in to change notification settings - Fork 256
/
UIImage+FontAwesome.m
147 lines (117 loc) · 5.13 KB
/
UIImage+FontAwesome.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
//
// UIImage+UIImage_FontAwesome.m
// FontAwesome-iOS Demo
//
// Created by Pedro Piñera Buendía on 22/08/13.
// Copyright (c) 2013 Alex Usbergo. All rights reserved.
//
#import "UIImage+FontAwesome.h"
#import "NSString+FontAwesome.h"
int fa_constraintLabelToSize(UILabel *label, CGSize size, int maxFontSize, int minFontSize)
{
// Set the frame of the label to the targeted rectangle
CGRect rect = CGRectMake(0, 0, size.width, size.height);
label.frame = rect;
// Try all font sizes from largest to smallest font size
int fontSize = maxFontSize;
// Fit label width wize
CGSize constraintSize = CGSizeMake(label.frame.size.width, MAXFLOAT);
do {
// Set current font size
label.font = [UIFont fontWithName:label.font.fontName size:fontSize];
// Find label size for current font size
CGRect textRect = [[label text] boundingRectWithSize:constraintSize
options:NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName:label.font}
context:nil];
// Done, if created label is within target size
if( textRect.size.height <= label.frame.size.height )
break;
// Decrease the font size and try again
fontSize -= 2;
} while (fontSize > minFontSize);
return fontSize;
}
@implementation UIImage (UIImage_FontAwesome)
+(UIImage*)imageWithIcon:(NSString*)identifier backgroundColor:(UIColor*)bgColor iconColor:(UIColor*)iconColor andSize:(CGSize)size{
if (!bgColor) {
bgColor = [UIColor clearColor];
}
if (!iconColor) {
iconColor = [UIColor whiteColor];
}
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
//// Abstracted Attributes
NSString* textContent = identifier;
if (identifier.length > 3 && [[identifier substringToIndex:3] isEqualToString:@"fa-"]) {
textContent = [NSString fontAwesomeIconStringForIconIdentifier:identifier];
}
CGRect textRect = CGRectZero;
textRect.size = size;
//// Retangle Drawing
UIBezierPath *path = [UIBezierPath bezierPathWithRect:textRect];
[bgColor setFill];
[path fill];
//// Text Drawing
int fontSize = size.width;
UIFont *font = [UIFont fontWithName:kFontAwesomeFamilyName size:fontSize];
@autoreleasepool {
UILabel *label = [UILabel new];
label.font = font;
label.text = textContent;
fontSize = fa_constraintLabelToSize(label, size, 500, 5);
font = label.font;
}
[iconColor setFill];
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.alignment = NSTextAlignmentCenter;
[textContent drawInRect:textRect withAttributes:@{NSFontAttributeName : font,
NSForegroundColorAttributeName : iconColor,
NSBackgroundColorAttributeName : bgColor,
NSParagraphStyleAttributeName: style,
}];
//Image returns
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+(UIImage*)imageWithIcon:(NSString*)identifier backgroundColor:(UIColor*)bgColor iconColor:(UIColor*)iconColor fontSize:(int)fontSize
{
if (!bgColor) {
bgColor = [UIColor clearColor];
}
if (!iconColor) {
iconColor = [UIColor whiteColor];
}
//// Abstracted Attributes
NSString* textContent = identifier;
if (identifier.length > 3 && [[identifier substringToIndex:3] isEqualToString:@"fa-"]) {
textContent = [NSString fontAwesomeIconStringForIconIdentifier:identifier];
}
UIFont *font = [UIFont fontWithName:kFontAwesomeFamilyName size:fontSize];
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = @{NSFontAttributeName : font,
NSForegroundColorAttributeName : iconColor,
NSBackgroundColorAttributeName : bgColor,
NSParagraphStyleAttributeName: style,
};
//// Content Edge Insets
CGSize size = [textContent sizeWithAttributes:attributes];
size = CGSizeMake(size.width * 1.1, size.height * 1.05);
CGRect textRect = CGRectZero;
textRect.size = size;
CGPoint origin = CGPointMake(size.width * 0.05, size.height * 0.025);
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
//// Rectangle Drawing
UIBezierPath *path = [UIBezierPath bezierPathWithRect:textRect];
[bgColor setFill];
[path fill];
//// Text Drawing
[textContent drawAtPoint:origin withAttributes:attributes];
//Image returns
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end