Skip to content

Commit 7b3e67d

Browse files
benviumMartin Konicek
authored and
Martin Konicek
committed
<Text> Expose Android's includeFontPadding property to JavaScript.
Summary: By default Android will put extra space above text to allow for upper-case accents or other ascenders. With some fonts, this can make text look slightly misaligned when centered vertically. We have found that the effect is very noticeable with certain custom fonts on Android. On iOS the font aligns vertically as expected. Android exposes a property `includeFontPadding` that will remove this extra padding if set to false. This PR exposes that to JS, and adds it to the documentation and UIExplorer. Closes #9323 Differential Revision: D4266713 Pulled By: lacker fbshipit-source-id: f9711254bc26c09b4586a865f0e95ef4bf77cf3f
1 parent 7fe5229 commit 7b3e67d

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

Examples/UIExplorer/js/TextExample.android.js

+25
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,23 @@ class TextExample extends React.Component {
411411
This very long text should be truncated with dots in the beginning.
412412
</Text>
413413
</UIExplorerBlock>
414+
<UIExplorerBlock title="Include Font Padding">
415+
<View style={{flexDirection: 'row', justifyContent: 'space-around', marginBottom: 10}}>
416+
<View style={{alignItems: 'center'}}>
417+
<Text style={styles.includeFontPaddingText}>
418+
Ey
419+
</Text>
420+
<Text>Default</Text>
421+
</View>
422+
<View style={{alignItems: 'center'}}>
423+
<Text style={[styles.includeFontPaddingText, {includeFontPadding: false, marginLeft: 10}]}>
424+
Ey
425+
</Text>
426+
<Text>includeFontPadding: false</Text>
427+
</View>
428+
</View>
429+
<Text>By default Android will put extra space above text to allow for upper-case accents or other ascenders. With some fonts, this can make text look slightly misaligned when centered vertically.</Text>
430+
</UIExplorerBlock>
414431
</UIExplorerPage>
415432
);
416433
}
@@ -421,6 +438,14 @@ var styles = StyleSheet.create({
421438
left: 5,
422439
backgroundColor: 'rgba(100, 100, 100, 0.3)'
423440
},
441+
includeFontPaddingText: {
442+
fontSize: 120,
443+
fontFamily: 'sans-serif',
444+
backgroundColor: '#EEEEEE',
445+
color: '#000000',
446+
textAlignVertical: 'center',
447+
alignSelf: 'center',
448+
}
424449
});
425450

426451
module.exports = TextExample;

Libraries/Text/TextStylePropTypes.js

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ const TextStylePropTypes = {
6666
textAlignVertical: ReactPropTypes.oneOf(
6767
['auto' /*default*/, 'top', 'bottom', 'center']
6868
),
69+
/**
70+
* Set to `false` to remove extra font padding intended to make space for certain ascenders / descenders.
71+
* With some fonts, this padding can make text look slightly misaligned when centered vertically.
72+
* For best results also set `textAlignVertical` to `center`. Default is true.
73+
* @platform android
74+
*/
75+
includeFontPadding: ReactPropTypes.bool,
6976
textDecorationLine: ReactPropTypes.oneOf(
7077
['none' /*default*/, 'underline', 'line-through', 'underline line-through']
7178
),

ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewManager.java

+5
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ public void setBorderColor(ReactTextView view, int index, Integer color) {
143143
view.setBorderColor(SPACING_TYPES[index], rgbComponent, alphaComponent);
144144
}
145145

146+
@ReactProp(name = "includeFontPadding", defaultBoolean = true)
147+
public void setIncludeFontPadding(ReactTextView view, boolean includepad) {
148+
view.setIncludeFontPadding(includepad);
149+
}
150+
146151
@Override
147152
public void updateExtraData(ReactTextView view, Object extraData) {
148153
ReactTextUpdate update = (ReactTextUpdate) extraData;

0 commit comments

Comments
 (0)