Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FontAwesome 5 #776

Merged
merged 23 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5340877
FontAwesome5 implementation
hampustagerud Jul 16, 2018
8e789bf
Update README.md
hampustagerud Jul 17, 2018
13e644a
Easier way to use FontAwesome5 Pro
hampustagerud Jul 17, 2018
c682035
FontAwesome 5 instructions
hampustagerud Jul 17, 2018
a698fe1
Automatic build and upgrading, also added to directory and IconExplorer
hampustagerud Jul 17, 2018
5d2dae1
Buttons and TabBarItems should be working on iOS too now
hampustagerud Jul 17, 2018
b144027
Updated build flow
hampustagerud Jul 18, 2018
5c1e203
Upgrading FontAwesome is automatic and occurs locally
hampustagerud Jul 18, 2018
0f9b2a5
Create FontAwesome 5 iconSet with factory
hampustagerud Jul 18, 2018
f1ef199
Setup FontAwesome 5 automatically
hampustagerud Jul 18, 2018
8a8a1d1
Bug fixes with FontAwesome 5 loading
hampustagerud Jul 18, 2018
569473f
getImageSource() implementation for FontAwesome 5
hampustagerud Jul 18, 2018
47cece2
Export all types in FontAwesome5 iconSets
hampustagerud Jul 18, 2018
6fd8697
Changed name on FA5Type to FA5Style
hampustagerud Jul 18, 2018
50942b8
Update docs to reflect changes to FontAwesome 5
hampustagerud Jul 18, 2018
d952755
Some final iOS fixes for FontAwesome 5 PR
hampustagerud Jul 18, 2018
f6d6365
Updated scripts
hampustagerud Jul 19, 2018
6bd1e49
Name changes and minor fixes
hampustagerud Jul 19, 2018
8c3189e
ensureNativeModuleAvailable() in its own file
hampustagerud Jul 19, 2018
7eb5d31
Removed add-font-assets from user binaries
hampustagerud Jul 19, 2018
4d1503a
Edited package.json ends with blank line now
hampustagerud Jul 19, 2018
068dc30
Cleaner code
hampustagerud Jul 19, 2018
d8763ee
Docs update
hampustagerud Jul 19, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Examples/IconExplorer/icon-sets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Entypo from 'react-native-vector-icons/Entypo';
import EvilIcons from 'react-native-vector-icons/EvilIcons';
import Feather from 'react-native-vector-icons/Feather';
import FontAwesome from 'react-native-vector-icons/FontAwesome';
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think you forgot to also add the font and integrate the library on iOS.

import Foundation from 'react-native-vector-icons/Foundation';
import Ionicons from 'react-native-vector-icons/Ionicons';
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
Expand All @@ -13,6 +14,7 @@ import EntypoGlyphs from 'react-native-vector-icons/glyphmaps/Entypo.json';
import EvilIconsGlyphs from 'react-native-vector-icons/glyphmaps/EvilIcons.json';
import FeatherGlyphs from 'react-native-vector-icons/glyphmaps/Feather.json';
import FontAwesomeGlyphs from 'react-native-vector-icons/glyphmaps/FontAwesome.json';
import FontAwesome5Glyphs from 'react-native-vector-icons/glyphmaps/FontAwesome5.json';
import FoundationGlyphs from 'react-native-vector-icons/glyphmaps/Foundation.json';
import IoniconsGlyphs from 'react-native-vector-icons/glyphmaps/Ionicons.json';
import MaterialIconsGlyphs from 'react-native-vector-icons/glyphmaps/MaterialIcons.json';
Expand All @@ -25,6 +27,7 @@ const GLYPH_MAPS = {
EvilIcons: EvilIconsGlyphs,
Feather: FeatherGlyphs,
FontAwesome: FontAwesomeGlyphs,
FontAwesome5: FontAwesome5Glyphs.free,
Foundation: FoundationGlyphs,
Ionicons: IoniconsGlyphs,
MaterialIcons: MaterialIconsGlyphs,
Expand All @@ -38,6 +41,7 @@ const ICON_SETS = {
EvilIcons,
Feather,
FontAwesome,
FontAwesome5,
Foundation,
Ionicons,
MaterialIcons,
Expand Down
104 changes: 103 additions & 1 deletion Examples/IconExplorer/ios/IconExplorer.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions Examples/IconExplorer/ios/IconExplorer/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
<string>MaterialCommunityIcons.ttf</string>
<string>Octicons.ttf</string>
<string>Zocial.ttf</string>
<string>FontAwesome5_Brands.ttf</string>
<string>FontAwesome5_Regular.ttf</string>
<string>FontAwesome5_Solid.ttf</string>
<string>SimpleLineIcons.ttf</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
Expand All @@ -52,9 +56,8 @@
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<string/>
<key>NSAppTransportSecurity</key>
<!--See http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/ -->
<dict>
<key>NSExceptionDomains</key>
<dict>
Expand Down
99 changes: 99 additions & 0 deletions FONTAWESOME5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# FontAwesome 5

FontAwesome 5 is split into three different font files which makes it somewhat
harder to use in some cases. The implemented solution should be fairly
straightforward to use after it has been setup.
Android and iOS handles fonts differently which is why it could be
easily implemented without any additional setup.

### Table of Content

* [`Usage`](#usage)
* [`Upgrading to Pro`](#upgrading-to-pro)

# Usage

Using the standard icons works just like the standard icons in this library.

```javascript
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5';

const icon = (<FontAwesome5 name={'comments'} />);
```

Something special about the FontAwesome5 class is that you can also pass props
to change the style of the icon:

```javascript
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5';

const icon = (<FontAwesome5 name={'comments'} solid />);
const icon = (<FontAwesome5 name={'git'} brand />);
```

**Valid types**

| Type | Description |
| --- | --- |
| **brand** | Uses the Brands font |
| **light** | Uses the Light font (pro) or Regular (Free) |
| **solid** | Uses the Solid font |

No specified type indicates Regular font.

Button, TabBarItem etc. works the same way:

```javascript
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5';

const regular_icon_btn = (<FontAwesome5.Button name={'comments'} />);
const solid_icon_btn = (<FontAwesome5.Button name={'comments'} solid />);
```

## getImageSource

```getImageSource``` works a little different due to its native backend and how
the font is separated into different files. Therefore, the enum FA5Style is
defined to help setting the style of the font:

```javascript
const FA5Style = {
regular: 0,
light: 1,
solid: 2,
brand: 3,
};
```

Use this to select which style the generated image should have:

```javascript
import FontAwesome5, { FA5Style } from 'react-native-vector-icons/FontAwesome5';

FontAwesome5.getImageSource("comments", 30, '#000', FA5Style.solid).then(source => this.setState({ image: source }));
```

Not passing a style will result in Regular style.

# Upgrading to Pro

You need your FontAwesome npm token which can be obtained by logging into your
account and then access the ```Services``` tab.

Run ```./node_modules/.bin/fa5-upgrade``` and enter the token when asked to in order to
upgrade to the Pro version.

## Manually

If the shell script does not work you can install the Pro version manually.
All you really need to do is adding the Pro fonts to your project, there is
instructions on how to do this in main README.md.

## Using the Pro version

Just as easy as using the Free icons, just include the icon set like this:
```javascript
import FontAwesome5Pro from 'react-native-vector-icons/FontAwesome5Pro';

const icon = (<FontAwesome5Pro name={'comments'} light/>);
```
20 changes: 20 additions & 0 deletions FontAwesome5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think you should separate this out to its own file just for the factory function.

* FontAwesome5 icon set component.
* Usage: <FontAwesome5 name="icon-name" size={20} color="#4F8EF7" />
*/

import { createFA5iconSet } from './lib/create-icon-set-from-fontawesome5';

import glyphMap from './glyphmaps/FontAwesome5Free.json';

export { FA5Style } from './lib/create-icon-set-from-fontawesome5';

const iconSet = createFA5iconSet(glyphMap, false);

export default iconSet;

export const Button = iconSet.Button;
export const TabBarItem = iconSet.TabBarItem;
export const TabBarItemIOS = iconSet.TabBarItemIOS;
export const ToolbarAndroid = iconSet.ToolbarAndroid;
export const getImageSource = iconSet.getImageSource;
20 changes: 20 additions & 0 deletions FontAwesome5Pro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* FontAwesome5Pro icon set component.
* Usage: <FontAwesome5Pro name="icon-name" size={20} color="#4F8EF7" />
*/

import { createFA5iconSet } from './lib/create-icon-set-from-fontawesome5';

import glyphMap from './glyphmaps/FontAwesome5Pro.json';

export { FA5Style } from './lib/create-icon-set-from-fontawesome5';

const iconSet = createFA5iconSet(glyphMap, true);

export default iconSet;

export const Button = iconSet.Button;
export const TabBarItem = iconSet.TabBarItem;
export const TabBarItemIOS = iconSet.TabBarItemIOS;
export const ToolbarAndroid = iconSet.ToolbarAndroid;
export const getImageSource = iconSet.getImageSource;
Binary file added Fonts/FontAwesome5_Brands.ttf
Binary file not shown.
Binary file added Fonts/FontAwesome5_Regular.ttf
Binary file not shown.
Binary file added Fonts/FontAwesome5_Solid.ttf
Binary file not shown.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Perfect for buttons, logos and nav/tab bars. Easy to extend, style and integrate
* [`Entypo`](http://entypo.com) by Daniel Bruce (**411** icons)
* [`EvilIcons`](http://evil-icons.io) by Alexander Madyankin & Roman Shamin (v1.8.0, **70** icons)
* [`Feather`](http://feathericons.com) by Cole Bemis & Contributors (v4.7.0, **266** icons)
* [`FontAwesome`](http://fortawesome.github.io/Font-Awesome/icons/) by Dave Gandy (v4.7.0, **675** icons)
* [`FontAwesome`](http://fortawesome.github.io/Font-Awesome/icons/) by Dave Gandy (v4.7.0, **675** icons)
* [`FontAwesome 5`](https://fontawesome.com) by Fonticons, Inc. (v5.1.1, 1265 (free) **2067** (pro) icons)
* [`Foundation`](http://zurb.com/playground/foundation-icon-fonts-3) by ZURB, Inc. (v3.0, **283** icons)
* [`Ionicons`](https://ionicons.com/) by Ben Sperry (v4.2.4, **696** icons)
* [`MaterialIcons`](https://www.google.com/design/icons/) by Google, Inc. (v3.0.1, **932** icons)
Expand All @@ -24,6 +25,7 @@ Perfect for buttons, logos and nav/tab bars. Easy to extend, style and integrate

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

### iOS

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

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

9 changes: 9 additions & 0 deletions RNVectorIconsManager/RNVectorIconsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@

@interface RNVectorIconsManager : NSObject <RCTBridgeModule>

- (NSString *)hexStringFromColor:(UIColor *)color;
- (NSString *)generateFilePath:(NSString *)glyph withFontName:(NSString *)fontName
withFontSize:(CGFloat)fontSize
withColor:(UIColor *)color
withExtraIdentifier:(NSString *)identifier;
- (BOOL)createAndSaveGlyphImage:(NSString *)glyph withFont:(UIFont *)font
withFilePath:(NSString *)filePath
withColor:(UIColor *)color;

@end
114 changes: 103 additions & 11 deletions RNVectorIconsManager/RNVectorIconsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ @implementation RNVectorIconsManager
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE();

- (NSString *)hexStringFromColor:(UIColor *)color {
- (NSString *)hexStringFromColor:(UIColor *)color
{
const CGFloat *components = CGColorGetComponents(color.CGColor);

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


RCT_EXPORT_METHOD(getImageForFont:(NSString*)fontName withGlyph:(NSString*)glyph withFontSize:(CGFloat)fontSize withColor:(UIColor *)color callback:(RCTResponseSenderBlock)callback){
- (NSString *)generateFilePath:(NSString *)glyph withFontName:(NSString *)fontName
withFontSize:(CGFloat)fontSize
withColor:(UIColor *)color
withExtraIdentifier:(NSString *)identifier
{
CGFloat screenScale = RCTScreenScale();

NSString *hexColor = [self hexStringFromColor:color];
NSString *fileName = [NSString stringWithFormat:@"tmp/RNVectorIcons_%@_%@_%hu_%.f%@@%.fx.png",
identifier, fontName,
[glyph characterAtIndex:0],
fontSize, hexColor, screenScale];

return [NSHomeDirectory() stringByAppendingPathComponent:fileName];
}

NSString *fileName = [NSString stringWithFormat:@"tmp/RNVectorIcons_%@_%hu_%.f%@@%.fx.png", fontName, [glyph characterAtIndex:0], fontSize, hexColor, screenScale];
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:fileName];

- (BOOL)createAndSaveGlyphImage:(NSString *)glyph withFont:(UIFont *)font
withFilePath:(NSString *)filePath
withColor:(UIColor *)color
{
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
// No cached icon exists, we need to create it and persist to disk

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

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

NSData *imageData = UIImagePNGRepresentation(iconImage);
BOOL success = [imageData writeToFile:filePath atomically:YES];
if(!success) {
return callback(@[@"Failed to write rendered icon image"]);
return [imageData writeToFile:filePath atomically:YES];
}

return YES;
}

RCT_EXPORT_METHOD(getImageForFont:(NSString *)fontName
withGlyph:(NSString *)glyph
withFontSize:(CGFloat)fontSize
withColor:(UIColor *)color
callback:(RCTResponseSenderBlock)callback)
{
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
NSString *filePath = [self generateFilePath:glyph withFontName:fontName
withFontSize:fontSize
withColor:color
withExtraIdentifier:@""];

BOOL success = [self createAndSaveGlyphImage:glyph withFont:font
withFilePath:filePath
withColor:color];

if(!success) {
return callback(@[@"Failed to write rendered icon image"]);
}

callback(@[[NSNull null], filePath]);
}

RCT_EXPORT_METHOD(getImageForFontAwesome5:(NSString *)fontFamily
withGlyph:(NSString *)glyph
withFontSize:(CGFloat)fontSize
withFontStyle:(NSInteger)style
withColor:(UIColor *)color
callback:(RCTResponseSenderBlock)callback)
{
NSNumber *fontWeight = [NSNumber numberWithDouble:UIFontWeightRegular];
if (style == 1)
fontWeight = [NSNumber numberWithDouble:UIFontWeightUltraLight];
else if (style == 2)
fontWeight = [NSNumber numberWithDouble:UIFontWeightBold];

NSString *identifier = [NSString stringWithFormat:@"FA5.%ld", (long)style];
NSString *filePath = [self generateFilePath:glyph withFontName:fontFamily
withFontSize:fontSize
withColor:color
withExtraIdentifier: identifier];

UIFont *font = [UIFont fontWithName:fontFamily size:fontSize];
for (NSString *fontString in [UIFont fontNamesForFamilyName:fontFamily]) {
UIFont *testFont = [UIFont fontWithName:fontString size:fontSize];
NSDictionary *traits = [testFont.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute];
NSNumber *testFontWeight = traits[UIFontWeightTrait];

if (testFontWeight.doubleValue == fontWeight.doubleValue) {
font = testFont;
break;
}
}

BOOL success = [self createAndSaveGlyphImage:glyph withFont:font
withFilePath:filePath
withColor:color];
if(!success) {
return callback(@[@"Failed to write rendered icon image"]);
}

callback(@[[NSNull null], filePath]);
}

Expand Down Expand Up @@ -105,4 +177,24 @@ - (NSString *)hexStringFromColor:(UIColor *)color {
}
}

RCT_EXPORT_METHOD(setupFontAwesome5)
{
for (NSString *family in [UIFont familyNames]) {
if ([family hasPrefix:@"Font Awesome 5"]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:family]) {
UIFont *font = [UIFont fontWithName:fontName size:12];
NSDictionary *traits = [font.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute];

if ([fontName hasSuffix:@"Light"]) {
[traits setValue:[NSNumber numberWithDouble:UIFontWeightUltraLight] forKey:UIFontWeightTrait];
} else if ([fontName hasSuffix:@"Regular"]) {
[traits setValue:[NSNumber numberWithDouble:UIFontWeightRegular] forKey:UIFontWeightTrait];
} else if ([fontName hasSuffix:@"Solid"]) {
[traits setValue:[NSNumber numberWithDouble:UIFontWeightBold] forKey:UIFontWeightTrait];
}
}
}
}
}

@end
Loading