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

Allow the fontweight for the title to be customized #706

Merged
merged 11 commits into from
Dec 7, 2020
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ Customize the font family to be used for the title.

Customize the size of the font to be used for the title.

#### `titleFontWeight`
dylancom marked this conversation as resolved.
Show resolved Hide resolved

Customize the weight of the font to be used for the title.

#### `titleColor`

Allows for setting text color of the title.
Expand Down
5 changes: 3 additions & 2 deletions TestsExample/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import Test649 from './src/Test649';
import Test654 from './src/Test654';
import Test691 from './src/Test691';
import Test702 from './src/Test702';
import Test706 from './src/Test706';
import Test713 from './src/Test713';

enableScreens();

export default function App() {
return <Test42 />;
};
return <Test706 />;
dylancom marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion TestsExample/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 123196e881422a7a7f3a9c228d6113e8de498360

COCOAPODS: 1.9.3
COCOAPODS: 1.10.0
dylancom marked this conversation as resolved.
Show resolved Hide resolved
50 changes: 50 additions & 0 deletions TestsExample/src/Test706.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React from 'react';
import {Button, Text, View} from 'react-native';
import {NavigationContainer} from '@react-navigation/native';
import {createNativeStackNavigator} from 'react-native-screens/native-stack';

function HomeScreen({navigation}) {
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
<Text style={{fontSize: 24}}>Home screen with a 'bold' title</Text>
<Button
onPress={() => navigation.navigate('Details')}
title="Go to Details"
/>
</View>
);
}

function DetailsScreen() {
return (
<View>
<Text>Details screen with a 'light' title</Text>
</View>
);
}

const RootStack = createNativeStackNavigator();

function RootStackScreen() {
return (
<RootStack.Navigator
screenOptions={{
headerTitleStyle: {fontWeight: '900'},
}}>
<RootStack.Screen name="Home" component={HomeScreen} />
<RootStack.Screen
name="Details"
component={DetailsScreen}
options={{headerTitleStyle: {fontWeight: '100'}}}
/>
</RootStack.Navigator>
);
}

export default function App() {
return (
<NavigationContainer>
<RootStackScreen />
</NavigationContainer>
);
}
1 change: 1 addition & 0 deletions createNativeStackNavigator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Style object for header title. Supported properties:

- `fontFamily`
- `fontSize`
- `fontWeight`
- `color`

#### `headerTopInsetEnabled`
Expand Down
1 change: 1 addition & 0 deletions ios/RNSScreenStackHeaderConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef NS_ENUM(NSInteger, RNSStatusBarStyle) {
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *titleFontFamily;
@property (nonatomic, retain) NSNumber *titleFontSize;
@property (nonatomic, retain) NSString *titleFontWeight;
dylancom marked this conversation as resolved.
Show resolved Hide resolved
@property (nonatomic, retain) UIColor *titleColor;
@property (nonatomic, retain) NSString *backTitle;
@property (nonatomic, retain) NSString *backTitleFontFamily;
Expand Down
17 changes: 11 additions & 6 deletions ios/RNSScreenStackHeaderConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,18 @@ + (void)setAnimatedConfig:(UIViewController *)vc withConfig:(RNSScreenStackHeade
[navbar setTranslucent:config.translucent];
[navbar setValue:@(hideShadow ? YES : NO) forKey:@"hidesShadow"];

if (config.titleFontFamily || config.titleFontSize || config.titleColor) {
if (config.titleFontFamily || config.titleFontSize || config.titleFontWeight || config.titleColor) {
NSMutableDictionary *attrs = [NSMutableDictionary new];

if (config.titleColor) {
attrs[NSForegroundColorAttributeName] = config.titleColor;
}

NSString *family = config.titleFontFamily ?: nil;
NSNumber *size = config.titleFontSize ?: @17;
if (config.titleFontFamily) {
attrs[NSFontAttributeName] = [RCTFont updateFont:nil withFamily:config.titleFontFamily size:size weight:nil style:nil variant:nil scaleMultiplier:1.0];
NSString *weight = config.titleFontWeight ?: nil;
if (config.titleFontFamily || config.titleFontWeight) {
dylancom marked this conversation as resolved.
Show resolved Hide resolved
attrs[NSFontAttributeName] = [RCTFont updateFont:nil withFamily:family size:size weight:weight style:nil variant:nil scaleMultiplier:1.0];
} else {
attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:[size floatValue]];
}
Expand Down Expand Up @@ -291,16 +293,18 @@ + (UINavigationBarAppearance*)buildAppearance:(UIViewController *)vc withConfig:
appearance.shadowColor = nil;
}

if (config.titleFontFamily || config.titleFontSize || config.titleColor) {
if (config.titleFontFamily || config.titleFontSize || config.titleFontWeight || config.titleColor) {
NSMutableDictionary *attrs = [NSMutableDictionary new];

if (config.titleColor) {
attrs[NSForegroundColorAttributeName] = config.titleColor;
}

NSString *family = config.titleFontFamily ?: nil;
NSNumber *size = config.titleFontSize ?: @17;
if (config.titleFontFamily) {
attrs[NSFontAttributeName] = [RCTFont updateFont:nil withFamily:config.titleFontFamily size:size weight:nil style:nil variant:nil scaleMultiplier:1.0];
NSString *weight = config.titleFontWeight ?: nil;
if (config.titleFontFamily || config.titleFontWeight) {
dylancom marked this conversation as resolved.
Show resolved Hide resolved
attrs[NSFontAttributeName] = [RCTFont updateFont:nil withFamily:config.titleFontFamily size:size weight:weight style:nil variant:nil scaleMultiplier:1.0];
} else {
attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:[size floatValue]];
}
Expand Down Expand Up @@ -559,6 +563,7 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(title, NSString)
RCT_EXPORT_VIEW_PROPERTY(titleFontFamily, NSString)
RCT_EXPORT_VIEW_PROPERTY(titleFontSize, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(titleFontWeight, NSString)
RCT_EXPORT_VIEW_PROPERTY(titleColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(backTitle, NSString)
RCT_EXPORT_VIEW_PROPERTY(backTitleFontFamily, NSString)
Expand Down
1 change: 1 addition & 0 deletions native-stack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Style object for header title. Supported properties:

- `fontFamily`
- `fontSize`
- `fontWeight`
- `color`

#### `headerTopInsetEnabled`
Expand Down
1 change: 1 addition & 0 deletions src/createNativeStackNavigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class StackView extends React.Component {
(headerTitleStyle && headerTitleStyle.color) || headerTintColor,
titleFontFamily: headerTitleStyle && headerTitleStyle.fontFamily,
titleFontSize: headerTitleStyle && headerTitleStyle.fontSize,
titleFontWeight: headerTitleStyle && headerTitleStyle.fontWeight,
translucent: translucent === undefined ? false : translucent,
};

Expand Down
4 changes: 4 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ declare module 'react-native-screens' {
* @description Customize the size of the font to be used for the title.
*/
titleFontSize?: number;
/**
* @description Customize the weight of the font to be used for the title.
*/
dylancom marked this conversation as resolved.
Show resolved Hide resolved
titleFontWeight?: string;
/**
* @host (Android only)
* @description A flag to that lets you opt out of insetting the header. You may want to set this to `false` if you use an opaque status bar. Defaults to `true`.
Expand Down
1 change: 1 addition & 0 deletions src/native-stack/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export type NativeStackNavigationOptions = {
headerTitleStyle?: {
fontFamily?: string;
fontSize?: number;
fontWeight?: string;
dylancom marked this conversation as resolved.
Show resolved Hide resolved
color?: string;
};
/**
Expand Down
1 change: 1 addition & 0 deletions src/native-stack/views/HeaderConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export default function HeaderConfig({
}
titleFontFamily={titleFontFamily}
titleFontSize={headerTitleStyle.fontSize}
titleFontWeight={headerTitleStyle.fontWeight}
topInsetEnabled={headerTopInsetEnabled}
translucent={headerTranslucent === true}>
{headerRight !== undefined ? (
Expand Down