Skip to content

Commit 1287bd3

Browse files
committed
AutoScaleText
1 parent 3e44bc5 commit 1287bd3

File tree

2 files changed

+114
-8
lines changed

2 files changed

+114
-8
lines changed

App/Components/AutoScaleText.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import React from 'react';
2+
3+
import ReactNative, {
4+
NativeModules,
5+
} from 'react-native';
6+
7+
const UIManager = NativeModules.UIManager;
8+
9+
import Text from './Text';
10+
11+
class AutoScaleText extends React.Component {
12+
static propTypes = {
13+
...Text.propTypes,
14+
maxFontSize: React.PropTypes.number.isRequired,
15+
maxHeight: React.PropTypes.number.isRequired,
16+
color: React.PropTypes.string,
17+
style: React.PropTypes.oneOfType([
18+
Text.propTypes.style,
19+
React.PropTypes.shape({
20+
width: React.PropTypes.number,
21+
}),
22+
]).isRequired,
23+
};
24+
25+
static defaultProps = {
26+
color: 'black',
27+
};
28+
29+
constructor(props) {
30+
super(props);
31+
32+
this.state = {
33+
fontSize: props.maxFontSize,
34+
finished: false,
35+
};
36+
}
37+
38+
determineFontSize = () => {
39+
UIManager.measure(ReactNative.findNodeHandle(this.refs.textView), (x, y, w, h, px, py) => {
40+
if (h > this.props.maxHeight) {
41+
this.setState({
42+
fontSize: this.state.fontSize - 0.5,
43+
});
44+
this.determineFontSize();
45+
}
46+
else {
47+
this.setState({finished: true});
48+
}
49+
});
50+
};
51+
52+
render() {
53+
return (
54+
<Text
55+
ref='textView'
56+
onLayout={this.determineFontSize}
57+
{...this.props}
58+
style={[
59+
this.props.style,
60+
{
61+
fontSize: this.state.fontSize,
62+
color: this.state.finished ? this.props.color : 'transparent',
63+
},
64+
]}
65+
>
66+
{this.props.children}
67+
</Text>
68+
);
69+
}
70+
}
71+
72+
export default AutoScaleText;

App/Navigation/NavigationTitle.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import React from 'react';
22
import {
33
View,
4-
StyleSheet
4+
StyleSheet,
5+
Dimensions,
6+
Platform,
57
} from 'react-native';
68

9+
const { width } = Dimensions.get('window');
10+
711
import cssVar from '../Lib/cssVar';
812

913
import DispatcherListener from '../Mixins/DispatcherListener';
1014
import AppConstants from '../Constants/AppConstants';
1115

12-
import Text from '../Components/Text';
16+
import AutoScaleText from '../Components/AutoScaleText';
1317

1418
var NavigationTitle = React.createClass({
1519
mixins: [DispatcherListener],
@@ -28,24 +32,54 @@ var NavigationTitle = React.createClass({
2832
}
2933
},
3034

35+
getButtonPadding() {
36+
const { route } = this.props;
37+
38+
// for some reason 70 is the magic number for Android
39+
if (Platform.OS === 'android') {
40+
return 70;
41+
}
42+
// if navRight is a text return 70
43+
if (route.navRight && route.navRight.label) {
44+
return 70;
45+
}
46+
// if navLeft is a text return 70
47+
if (route.navLeft && route.navLeft.label) {
48+
return 70;
49+
}
50+
51+
if (route.navBack && route.navBack.label) {
52+
return 70;
53+
}
54+
55+
return 40;
56+
},
57+
3158
render: function() {
3259
var title = this.state.updatedTitle || this.props.route.title;
3360
return (
34-
<Text style={styles.navBarTitleText}>
61+
<AutoScaleText
62+
style={[styles.navBarTitleText, {width: width - this.getButtonPadding() * 2}]}
63+
allowFontScaling={false}
64+
maxFontSize={20}
65+
maxHeight={50}
66+
color='white'
67+
>
3568
{title}
36-
</Text>
69+
</AutoScaleText>
3770
);
3871
}
3972
});
4073

74+
const marginVertical = Platform.OS === 'ios' ? 8 : 15;
4175
var styles = StyleSheet.create({
4276
navBarTitleText: {
43-
fontSize: 20,
4477
fontFamily: cssVar('fontRegular'),
45-
color: 'white',
4678
fontWeight: '500',
47-
marginVertical: 9,
48-
}
79+
marginVertical: marginVertical,
80+
textAlign: 'center',
81+
paddingBottom: 6,
82+
},
4983
});
5084

5185
export default NavigationTitle;

0 commit comments

Comments
 (0)