Skip to content

Commit 45e8617

Browse files
hampustagerudoblador
authored andcommitted
FontAwesome 5 (oblador#776)
* FontAwesome5 implementation + GlyphMap for FontAwesome 5.1.0 Pro + Font files for FontAwesome 5.1.0 Free + Implementation enabling FontAwesome5 icons Handles the copying and renaming of FontAwesome5 Pro font files Removed debug code * Update README.md * Easier way to use FontAwesome5 Pro Now a default import from a separate file, instead of creating it using FA5iconSet(bool). * FontAwesome 5 instructions Fixes to pass tests * Automatic build and upgrading, also added to directory and IconExplorer * Buttons and TabBarItems should be working on iOS too now * Updated build flow + FontAwesome 5.1.1 + Temporary folder and npm pack when building + Separate glyphmaps * Upgrading FontAwesome is automatic and occurs locally * Create FontAwesome 5 iconSet with factory * Setup FontAwesome 5 automatically * Bug fixes with FontAwesome 5 loading * getImageSource() implementation for FontAwesome 5 * Export all types in FontAwesome5 iconSets * Changed name on FA5Type to FA5Style * Update docs to reflect changes to FontAwesome 5 * Some final iOS fixes for FontAwesome 5 PR * Updated scripts * Name changes and minor fixes * ensureNativeModuleAvailable() in its own file * Removed add-font-assets from user binaries * Edited package.json ends with blank line now * Cleaner code * Docs update
1 parent 14be3c6 commit 45e8617

24 files changed

+3164
-36
lines changed

Diff for: Examples/IconExplorer/icon-sets.js

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Entypo from 'react-native-vector-icons/Entypo';
33
import EvilIcons from 'react-native-vector-icons/EvilIcons';
44
import Feather from 'react-native-vector-icons/Feather';
55
import FontAwesome from 'react-native-vector-icons/FontAwesome';
6+
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5';
67
import Foundation from 'react-native-vector-icons/Foundation';
78
import Ionicons from 'react-native-vector-icons/Ionicons';
89
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
@@ -13,6 +14,7 @@ import EntypoGlyphs from 'react-native-vector-icons/glyphmaps/Entypo.json';
1314
import EvilIconsGlyphs from 'react-native-vector-icons/glyphmaps/EvilIcons.json';
1415
import FeatherGlyphs from 'react-native-vector-icons/glyphmaps/Feather.json';
1516
import FontAwesomeGlyphs from 'react-native-vector-icons/glyphmaps/FontAwesome.json';
17+
import FontAwesome5Glyphs from 'react-native-vector-icons/glyphmaps/FontAwesome5.json';
1618
import FoundationGlyphs from 'react-native-vector-icons/glyphmaps/Foundation.json';
1719
import IoniconsGlyphs from 'react-native-vector-icons/glyphmaps/Ionicons.json';
1820
import MaterialIconsGlyphs from 'react-native-vector-icons/glyphmaps/MaterialIcons.json';
@@ -25,6 +27,7 @@ const GLYPH_MAPS = {
2527
EvilIcons: EvilIconsGlyphs,
2628
Feather: FeatherGlyphs,
2729
FontAwesome: FontAwesomeGlyphs,
30+
FontAwesome5: FontAwesome5Glyphs.free,
2831
Foundation: FoundationGlyphs,
2932
Ionicons: IoniconsGlyphs,
3033
MaterialIcons: MaterialIconsGlyphs,
@@ -38,6 +41,7 @@ const ICON_SETS = {
3841
EvilIcons,
3942
Feather,
4043
FontAwesome,
44+
FontAwesome5,
4145
Foundation,
4246
Ionicons,
4347
MaterialIcons,

Diff for: Examples/IconExplorer/ios/IconExplorer.xcodeproj/project.pbxproj

+103-1
Large diffs are not rendered by default.

Diff for: Examples/IconExplorer/ios/IconExplorer/Info.plist

+5-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
<string>MaterialCommunityIcons.ttf</string>
3737
<string>Octicons.ttf</string>
3838
<string>Zocial.ttf</string>
39+
<string>FontAwesome5_Brands.ttf</string>
40+
<string>FontAwesome5_Regular.ttf</string>
41+
<string>FontAwesome5_Solid.ttf</string>
42+
<string>SimpleLineIcons.ttf</string>
3943
</array>
4044
<key>UILaunchStoryboardName</key>
4145
<string>LaunchScreen</string>
@@ -52,9 +56,8 @@
5256
<key>UIViewControllerBasedStatusBarAppearance</key>
5357
<false/>
5458
<key>NSLocationWhenInUseUsageDescription</key>
55-
<string></string>
59+
<string/>
5660
<key>NSAppTransportSecurity</key>
57-
<!--See http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/ -->
5861
<dict>
5962
<key>NSExceptionDomains</key>
6063
<dict>

Diff for: FONTAWESOME5.md

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# FontAwesome 5
2+
3+
FontAwesome 5 is split into three different font files which makes it somewhat
4+
harder to use in some cases. The implemented solution should be fairly
5+
straightforward to use after it has been setup.
6+
Android and iOS handles fonts differently which is why it could be
7+
easily implemented without any additional setup.
8+
9+
### Table of Content
10+
11+
* [`Usage`](#usage)
12+
* [`Upgrading to Pro`](#upgrading-to-pro)
13+
14+
# Usage
15+
16+
Using the standard icons works just like the standard icons in this library.
17+
18+
```javascript
19+
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5';
20+
21+
const icon = (<FontAwesome5 name={'comments'} />);
22+
```
23+
24+
Something special about the FontAwesome5 class is that you can also pass props
25+
to change the style of the icon:
26+
27+
```javascript
28+
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5';
29+
30+
const icon = (<FontAwesome5 name={'comments'} solid />);
31+
const icon = (<FontAwesome5 name={'git'} brand />);
32+
```
33+
34+
**Valid types**
35+
36+
| Type | Description |
37+
| --- | --- |
38+
| **brand** | Uses the Brands font |
39+
| **light** | Uses the Light font (pro) or Regular (Free) |
40+
| **solid** | Uses the Solid font |
41+
42+
No specified type indicates Regular font.
43+
44+
Button, TabBarItem etc. works the same way:
45+
46+
```javascript
47+
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5';
48+
49+
const regular_icon_btn = (<FontAwesome5.Button name={'comments'} />);
50+
const solid_icon_btn = (<FontAwesome5.Button name={'comments'} solid />);
51+
```
52+
53+
## getImageSource
54+
55+
```getImageSource``` works a little different due to its native backend and how
56+
the font is separated into different files. Therefore, the enum FA5Style is
57+
defined to help setting the style of the font:
58+
59+
```javascript
60+
const FA5Style = {
61+
regular: 0,
62+
light: 1,
63+
solid: 2,
64+
brand: 3,
65+
};
66+
```
67+
68+
Use this to select which style the generated image should have:
69+
70+
```javascript
71+
import FontAwesome5, { FA5Style } from 'react-native-vector-icons/FontAwesome5';
72+
73+
FontAwesome5.getImageSource("comments", 30, '#000', FA5Style.solid).then(source => this.setState({ image: source }));
74+
```
75+
76+
Not passing a style will result in Regular style.
77+
78+
# Upgrading to Pro
79+
80+
You need your FontAwesome npm token which can be obtained by logging into your
81+
account and then access the ```Services``` tab.
82+
83+
Run ```./node_modules/.bin/fa5-upgrade``` and enter the token when asked to in order to
84+
upgrade to the Pro version.
85+
86+
## Manually
87+
88+
If the shell script does not work you can install the Pro version manually.
89+
All you really need to do is adding the Pro fonts to your project, there is
90+
instructions on how to do this in main README.md.
91+
92+
## Using the Pro version
93+
94+
Just as easy as using the Free icons, just include the icon set like this:
95+
```javascript
96+
import FontAwesome5Pro from 'react-native-vector-icons/FontAwesome5Pro';
97+
98+
const icon = (<FontAwesome5Pro name={'comments'} light/>);
99+
```

Diff for: FontAwesome5.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* FontAwesome5 icon set component.
3+
* Usage: <FontAwesome5 name="icon-name" size={20} color="#4F8EF7" />
4+
*/
5+
6+
import { createFA5iconSet } from './lib/create-icon-set-from-fontawesome5';
7+
8+
import glyphMap from './glyphmaps/FontAwesome5Free.json';
9+
10+
export { FA5Style } from './lib/create-icon-set-from-fontawesome5';
11+
12+
const iconSet = createFA5iconSet(glyphMap, false);
13+
14+
export default iconSet;
15+
16+
export const Button = iconSet.Button;
17+
export const TabBarItem = iconSet.TabBarItem;
18+
export const TabBarItemIOS = iconSet.TabBarItemIOS;
19+
export const ToolbarAndroid = iconSet.ToolbarAndroid;
20+
export const getImageSource = iconSet.getImageSource;

Diff for: FontAwesome5Pro.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* FontAwesome5Pro icon set component.
3+
* Usage: <FontAwesome5Pro name="icon-name" size={20} color="#4F8EF7" />
4+
*/
5+
6+
import { createFA5iconSet } from './lib/create-icon-set-from-fontawesome5';
7+
8+
import glyphMap from './glyphmaps/FontAwesome5Pro.json';
9+
10+
export { FA5Style } from './lib/create-icon-set-from-fontawesome5';
11+
12+
const iconSet = createFA5iconSet(glyphMap, true);
13+
14+
export default iconSet;
15+
16+
export const Button = iconSet.Button;
17+
export const TabBarItem = iconSet.TabBarItem;
18+
export const TabBarItemIOS = iconSet.TabBarItemIOS;
19+
export const ToolbarAndroid = iconSet.ToolbarAndroid;
20+
export const getImageSource = iconSet.getImageSource;

Diff for: Fonts/FontAwesome5_Brands.ttf

112 KB
Binary file not shown.

Diff for: Fonts/FontAwesome5_Regular.ttf

39.6 KB
Binary file not shown.

Diff for: Fonts/FontAwesome5_Solid.ttf

157 KB
Binary file not shown.

Diff for: README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Perfect for buttons, logos and nav/tab bars. Easy to extend, style and integrate
1111
* [`Entypo`](http://entypo.com) by Daniel Bruce (**411** icons)
1212
* [`EvilIcons`](http://evil-icons.io) by Alexander Madyankin & Roman Shamin (v1.8.0, **70** icons)
1313
* [`Feather`](http://feathericons.com) by Cole Bemis & Contributors (v4.7.0, **266** icons)
14-
* [`FontAwesome`](http://fortawesome.github.io/Font-Awesome/icons/) by Dave Gandy (v4.7.0, **675** icons)
14+
* [`FontAwesome`](http://fortawesome.github.io/Font-Awesome/icons/) by Dave Gandy (v4.7.0, **675** icons)
15+
* [`FontAwesome 5`](https://fontawesome.com) by Fonticons, Inc. (v5.1.1, 1265 (free) **2067** (pro) icons)
1516
* [`Foundation`](http://zurb.com/playground/foundation-icon-fonts-3) by ZURB, Inc. (v3.0, **283** icons)
1617
* [`Ionicons`](https://ionicons.com/) by Ben Sperry (v4.2.4, **696** icons)
1718
* [`MaterialIcons`](https://www.google.com/design/icons/) by Google, Inc. (v3.0.1, **932** icons)
@@ -24,6 +25,7 @@ Perfect for buttons, logos and nav/tab bars. Easy to extend, style and integrate
2425

2526
1. Run: `$ npm install react-native-vector-icons --save`
2627
2. For each platform (iOS/Android/Windows) you plan to use, follow one of the options for the corresponding platform.
28+
3. If you intend to use FontAwesome 5, check out [`this guide`](FONTAWESOME5.md) to get you started.
2729

2830
### iOS
2931

@@ -493,4 +495,3 @@ Both npm and android file hierarchies tend to get very deep and even worse when
493495
This project is licenced under the [MIT License](http://opensource.org/licenses/mit-license.html).
494496

495497
Any bundled fonts are copyright to their respective authors and mostly under MIT or [SIL OFL](http://scripts.sil.org/OFL).
496-

Diff for: RNVectorIconsManager/RNVectorIconsManager.h

+9
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,13 @@
1919

2020
@interface RNVectorIconsManager : NSObject <RCTBridgeModule>
2121

22+
- (NSString *)hexStringFromColor:(UIColor *)color;
23+
- (NSString *)generateFilePath:(NSString *)glyph withFontName:(NSString *)fontName
24+
withFontSize:(CGFloat)fontSize
25+
withColor:(UIColor *)color
26+
withExtraIdentifier:(NSString *)identifier;
27+
- (BOOL)createAndSaveGlyphImage:(NSString *)glyph withFont:(UIFont *)font
28+
withFilePath:(NSString *)filePath
29+
withColor:(UIColor *)color;
30+
2231
@end

Diff for: RNVectorIconsManager/RNVectorIconsManager.m

+103-11
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ @implementation RNVectorIconsManager
2929
@synthesize bridge = _bridge;
3030
RCT_EXPORT_MODULE();
3131

32-
- (NSString *)hexStringFromColor:(UIColor *)color {
32+
- (NSString *)hexStringFromColor:(UIColor *)color
33+
{
3334
const CGFloat *components = CGColorGetComponents(color.CGColor);
3435

3536
CGFloat r = components[0];
@@ -42,19 +43,28 @@ - (NSString *)hexStringFromColor:(UIColor *)color {
4243
lroundf(b * 255)];
4344
}
4445

45-
46-
RCT_EXPORT_METHOD(getImageForFont:(NSString*)fontName withGlyph:(NSString*)glyph withFontSize:(CGFloat)fontSize withColor:(UIColor *)color callback:(RCTResponseSenderBlock)callback){
46+
- (NSString *)generateFilePath:(NSString *)glyph withFontName:(NSString *)fontName
47+
withFontSize:(CGFloat)fontSize
48+
withColor:(UIColor *)color
49+
withExtraIdentifier:(NSString *)identifier
50+
{
4751
CGFloat screenScale = RCTScreenScale();
48-
4952
NSString *hexColor = [self hexStringFromColor:color];
53+
NSString *fileName = [NSString stringWithFormat:@"tmp/RNVectorIcons_%@_%@_%hu_%.f%@@%.fx.png",
54+
identifier, fontName,
55+
[glyph characterAtIndex:0],
56+
fontSize, hexColor, screenScale];
57+
58+
return [NSHomeDirectory() stringByAppendingPathComponent:fileName];
59+
}
5060

51-
NSString *fileName = [NSString stringWithFormat:@"tmp/RNVectorIcons_%@_%hu_%.f%@@%.fx.png", fontName, [glyph characterAtIndex:0], fontSize, hexColor, screenScale];
52-
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:fileName];
53-
61+
- (BOOL)createAndSaveGlyphImage:(NSString *)glyph withFont:(UIFont *)font
62+
withFilePath:(NSString *)filePath
63+
withColor:(UIColor *)color
64+
{
5465
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
5566
// No cached icon exists, we need to create it and persist to disk
5667

57-
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
5868
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:glyph attributes:@{NSFontAttributeName: font, NSForegroundColorAttributeName: color}];
5969

6070
CGSize iconSize = [attributedString size];
@@ -65,11 +75,73 @@ - (NSString *)hexStringFromColor:(UIColor *)color {
6575
UIGraphicsEndImageContext();
6676

6777
NSData *imageData = UIImagePNGRepresentation(iconImage);
68-
BOOL success = [imageData writeToFile:filePath atomically:YES];
69-
if(!success) {
70-
return callback(@[@"Failed to write rendered icon image"]);
78+
return [imageData writeToFile:filePath atomically:YES];
79+
}
80+
81+
return YES;
82+
}
83+
84+
RCT_EXPORT_METHOD(getImageForFont:(NSString *)fontName
85+
withGlyph:(NSString *)glyph
86+
withFontSize:(CGFloat)fontSize
87+
withColor:(UIColor *)color
88+
callback:(RCTResponseSenderBlock)callback)
89+
{
90+
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
91+
NSString *filePath = [self generateFilePath:glyph withFontName:fontName
92+
withFontSize:fontSize
93+
withColor:color
94+
withExtraIdentifier:@""];
95+
96+
BOOL success = [self createAndSaveGlyphImage:glyph withFont:font
97+
withFilePath:filePath
98+
withColor:color];
99+
100+
if(!success) {
101+
return callback(@[@"Failed to write rendered icon image"]);
102+
}
103+
104+
callback(@[[NSNull null], filePath]);
105+
}
106+
107+
RCT_EXPORT_METHOD(getImageForFontAwesome5:(NSString *)fontFamily
108+
withGlyph:(NSString *)glyph
109+
withFontSize:(CGFloat)fontSize
110+
withFontStyle:(NSInteger)style
111+
withColor:(UIColor *)color
112+
callback:(RCTResponseSenderBlock)callback)
113+
{
114+
NSNumber *fontWeight = [NSNumber numberWithDouble:UIFontWeightRegular];
115+
if (style == 1)
116+
fontWeight = [NSNumber numberWithDouble:UIFontWeightUltraLight];
117+
else if (style == 2)
118+
fontWeight = [NSNumber numberWithDouble:UIFontWeightBold];
119+
120+
NSString *identifier = [NSString stringWithFormat:@"FA5.%ld", (long)style];
121+
NSString *filePath = [self generateFilePath:glyph withFontName:fontFamily
122+
withFontSize:fontSize
123+
withColor:color
124+
withExtraIdentifier: identifier];
125+
126+
UIFont *font = [UIFont fontWithName:fontFamily size:fontSize];
127+
for (NSString *fontString in [UIFont fontNamesForFamilyName:fontFamily]) {
128+
UIFont *testFont = [UIFont fontWithName:fontString size:fontSize];
129+
NSDictionary *traits = [testFont.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute];
130+
NSNumber *testFontWeight = traits[UIFontWeightTrait];
131+
132+
if (testFontWeight.doubleValue == fontWeight.doubleValue) {
133+
font = testFont;
134+
break;
71135
}
72136
}
137+
138+
BOOL success = [self createAndSaveGlyphImage:glyph withFont:font
139+
withFilePath:filePath
140+
withColor:color];
141+
if(!success) {
142+
return callback(@[@"Failed to write rendered icon image"]);
143+
}
144+
73145
callback(@[[NSNull null], filePath]);
74146
}
75147

@@ -105,4 +177,24 @@ - (NSString *)hexStringFromColor:(UIColor *)color {
105177
}
106178
}
107179

180+
RCT_EXPORT_METHOD(setupFontAwesome5)
181+
{
182+
for (NSString *family in [UIFont familyNames]) {
183+
if ([family hasPrefix:@"Font Awesome 5"]) {
184+
for (NSString *fontName in [UIFont fontNamesForFamilyName:family]) {
185+
UIFont *font = [UIFont fontWithName:fontName size:12];
186+
NSDictionary *traits = [font.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute];
187+
188+
if ([fontName hasSuffix:@"Light"]) {
189+
[traits setValue:[NSNumber numberWithDouble:UIFontWeightUltraLight] forKey:UIFontWeightTrait];
190+
} else if ([fontName hasSuffix:@"Regular"]) {
191+
[traits setValue:[NSNumber numberWithDouble:UIFontWeightRegular] forKey:UIFontWeightTrait];
192+
} else if ([fontName hasSuffix:@"Solid"]) {
193+
[traits setValue:[NSNumber numberWithDouble:UIFontWeightBold] forKey:UIFontWeightTrait];
194+
}
195+
}
196+
}
197+
}
198+
}
199+
108200
@end

0 commit comments

Comments
 (0)