Skip to content
Permalink
Browse files

Allow substituting a default font handler

Reviewed By: nscoding

Differential Revision: D6798995

fbshipit-source-id: 541b4391428d1ee9c0b394033182dc232536c864
  • Loading branch information...
mmmulani authored and facebook-github-bot committed Jan 30, 2018
1 parent 22efd95 commit a9c684a0ff45852087310d5218062acfdab673f7
Showing with 59 additions and 2 deletions.
  1. +10 −0 React/Views/RCTFont.h
  2. +49 −2 React/Views/RCTFont.mm
@@ -11,6 +11,16 @@

#import <React/RCTConvert.h>

typedef UIFont *(^RCTFontHandler)(CGFloat fontSize, NSString *fontWeightDescription);

/**
* React Native will use the System font for rendering by default. If you want to
* provide a different base font, use this override. The font weight supplied to your
* handler will be one of "ultralight", "thin", "light", "regular", "medium",
* "semibold", "extrabold", "bold", "heavy", or "black".
*/
RCT_EXTERN void RCTSetDefaultFontHandler(RCTFontHandler handler);

@interface RCTFont : NSObject

/**
@@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

#import "RCTAssert.h"
#import "RCTFont.h"
#import "RCTLog.h"

@@ -96,6 +97,49 @@ static BOOL isCondensedFont(UIFont *font)
return (symbolicTraits & UIFontDescriptorTraitCondensed) != 0;
}

static RCTFontHandler defaultFontHandler;

void RCTSetDefaultFontHandler(RCTFontHandler handler) {
defaultFontHandler = handler;
}

// We pass a string description of the font weight to the defaultFontHandler because UIFontWeight
// is not defined pre-iOS 8.2.
// Furthermore, UIFontWeight's are lossy floats, so we must use an inexact compare to figure out
// which one we actually have.
static inline BOOL CompareFontWeights(UIFontWeight firstWeight, UIFontWeight secondWeight) {
#if CGFLOAT_IS_DOUBLE
return fabs(firstWeight - secondWeight) < 0.01;
#else
return fabsf(firstWeight - secondWeight) < 0.01;
#endif
}

static NSString *FontWeightDescriptionFromUIFontWeight(UIFontWeight fontWeight)
{
if (CompareFontWeights(fontWeight, UIFontWeightUltraLight)) {
return @"ultralight";
} else if (CompareFontWeights(fontWeight, UIFontWeightThin)) {
return @"thin";
} else if (CompareFontWeights(fontWeight, UIFontWeightLight)) {
return @"light";
} else if (CompareFontWeights(fontWeight, UIFontWeightRegular)) {
return @"regular";
} else if (CompareFontWeights(fontWeight, UIFontWeightMedium)) {
return @"medium";
} else if (CompareFontWeights(fontWeight, UIFontWeightSemibold)) {
return @"semibold";
} else if (CompareFontWeights(fontWeight, UIFontWeightBold)) {
return @"bold";
} else if (CompareFontWeights(fontWeight, UIFontWeightHeavy)) {
return @"heavy";
} else if (CompareFontWeights(fontWeight, UIFontWeightBlack)) {
return @"black";
}
RCTAssert(NO, @"Unknown UIFontWeight passed in: %f", fontWeight);
return @"regular";
}

static UIFont *cachedSystemFont(CGFloat size, RCTFontWeight weight)
{
static NSCache *fontCache;
@@ -112,8 +156,11 @@ static BOOL isCondensedFont(UIFont *font)
}

if (!font) {
// Only supported on iOS8.2 and above
if ([UIFont respondsToSelector:@selector(systemFontOfSize:weight:)]) {
if (defaultFontHandler) {
NSString *fontWeightDescription = FontWeightDescriptionFromUIFontWeight(weight);
font = defaultFontHandler(size, fontWeightDescription);
} else if ([UIFont respondsToSelector:@selector(systemFontOfSize:weight:)]) {
// Only supported on iOS8.2 and above
font = [UIFont systemFontOfSize:size weight:weight];
} else {
if (weight >= UIFontWeightBold) {

0 comments on commit a9c684a

Please sign in to comment.
You can’t perform that action at this time.