Skip to content

Commit 5898817

Browse files
motiz88facebook-github-bot
authored andcommitted
Implement letterSpacing on Android >= 5.0
Summary: `letterSpacing` is completely missing from RN Android at the moment. I've reviewed the `letterSpacing` implementations in #13199, #13877 and #16801 (that all seem to have stalled) and managed to put together an improved one based on #13199, updated to merge cleanly post 6114f86, that resolves the [issues](#13199 (comment)) I've identified with that code. I believe this is the closest PR yet to a correct implementation of this feature, with a few caveats: - As with the other PRs, this only works on Android >= 5.0 (silently falling back to no letter spacing on older versions). Is this acceptable for a RN feature, in general? Would a dev mode warning be desirable? - The other PRs seem to have explored the space of potential solutions to the layout issue ([Android renders space _around_ glyphs](https://issuetracker.google.com/issues/37079859), iOS to the _right_ of each one) and come up empty, so I've opted to merely document the difference. - I have neither updated nor tested the "Flat" UI implementation - everything compiles but I've taken [this comment](#12770 (comment)) to mean there's no point in trying to wade through it on my own right now; I'm happy to tackle it if given some pointers. - The implementation in `ReactEditText` is only there to handle the placeholder text, as `ReactBaseTextShadowNode` already affects the input control's contents correctly. - I'm not sure whether `<TextInput>` is meant to respect `allowFontScaling`; I've taken my cue here from `ReactTextInputManager.setFontSize()`, and used the same units (SP) to interpret the value in `ReactEditText.setLetterSpacingPt()`. - I'm not sure whether `<TextInput>` is even meant to support `letterSpacing` - it doesn't actually work on iOS. I'm not going to be able to handle the Objective-C side of this, not as part of this PR at least. - I have not added unit tests to `ReactTextTest` - is this desirable? I see that some other props such as `lineHeight` aren't covered there (unless I'm not looking in the right place). - Overall, I'm new to this codebase, so it's likely I've missed something not mentioned here. Note comment re: unit tests above; RNTester screenshots follow. | iOS (existing functionality, amended test) | Android (new functionality & test) | | - | - | | <img src=https://user-images.githubusercontent.com/2246565/34458459-c8d59498-edcb-11e7-8c8f-e7426f723886.png width=300> | <img src=https://user-images.githubusercontent.com/2246565/34458473-2a1ca368-edcc-11e7-9ce6-30c6d3a48660.png width=300> | | iOS _(not implemented, test not in this branch)_ | Android (new functionality & test) | | - | - | | <img src=https://user-images.githubusercontent.com/2246565/34458481-6c60a36e-edcc-11e7-9af5-9734dd722ced.png width=300> | <img src=https://user-images.githubusercontent.com/2246565/34458486-8b3cdcf8-edcc-11e7-974b-25c6085fa674.png width=300> | | iOS _(not implemented, test not in this branch)_ | Android (new functionality & test) | | - | - | | <img src=https://user-images.githubusercontent.com/2246565/34458492-d69a77be-edcc-11e7-896f-21212621dbee.png width=300> | <img src=https://user-images.githubusercontent.com/2246565/34458490-b3a1139e-edcc-11e7-88c8-79d4430d1514.png width=300> | facebook/react-native-website#105 - this docs PR is edited slightly from what's in `TextStylePropTypes` here; happy to align either one to the other after a review. [ANDROID] [FEATURE] [Text] - Implemented letterSpacing Closes #17398 Reviewed By: mdvacca Differential Revision: D6837718 Pulled By: hramos fbshipit-source-id: 5c9d49e9cf4af6457b636416ce5fe15315aab72c
1 parent dbafc29 commit 5898817

File tree

8 files changed

+176
-0
lines changed

8 files changed

+176
-0
lines changed

RNTester/js/TextExample.android.js

+30
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,36 @@ class TextExample extends React.Component<{}> {
341341
Holisticly formulate inexpensive ideas before best-of-breed benefits. <Text style={{fontSize: 20}}>Continually</Text> expedite magnetic potentialities rather than client-focused interfaces.
342342
</Text>
343343
</RNTesterBlock>
344+
<RNTesterBlock title="Letter Spacing">
345+
<View>
346+
<Text style={{letterSpacing: 0}}>
347+
letterSpacing = 0
348+
</Text>
349+
<Text style={{letterSpacing: 2, marginTop: 5}}>
350+
letterSpacing = 2
351+
</Text>
352+
<Text style={{letterSpacing: 9, marginTop: 5}}>
353+
letterSpacing = 9
354+
</Text>
355+
<View style={{flexDirection: 'row'}}>
356+
<Text style={{fontSize: 12, letterSpacing: 2, backgroundColor: 'fuchsia', marginTop: 5}}>
357+
With size and background color
358+
</Text>
359+
</View>
360+
<Text style={{letterSpacing: -1, marginTop: 5}}>
361+
letterSpacing = -1
362+
</Text>
363+
<Text style={{letterSpacing: 3, backgroundColor: '#dddddd', marginTop: 5}}>
364+
[letterSpacing = 3]
365+
<Text style={{letterSpacing: 0, backgroundColor: '#bbbbbb'}}>
366+
[Nested letterSpacing = 0]
367+
</Text>
368+
<Text style={{letterSpacing: 6, backgroundColor: '#eeeeee'}}>
369+
[Nested letterSpacing = 6]
370+
</Text>
371+
</Text>
372+
</View>
373+
</RNTesterBlock>
344374
<RNTesterBlock title="Empty Text">
345375
<Text />
346376
</RNTesterBlock>

RNTester/js/TextExample.ios.js

+14
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,23 @@ exports.examples = [
575575
<Text style={{letterSpacing: 9, marginTop: 5}}>
576576
letterSpacing = 9
577577
</Text>
578+
<View style={{flexDirection: 'row'}}>
579+
<Text style={{fontSize: 12, letterSpacing: 2, backgroundColor: 'fuchsia', marginTop: 5}}>
580+
With size and background color
581+
</Text>
582+
</View>
578583
<Text style={{letterSpacing: -1, marginTop: 5}}>
579584
letterSpacing = -1
580585
</Text>
586+
<Text style={{letterSpacing: 3, backgroundColor: '#dddddd', marginTop: 5}}>
587+
[letterSpacing = 3]
588+
<Text style={{letterSpacing: 0, backgroundColor: '#bbbbbb'}}>
589+
[Nested letterSpacing = 0]
590+
</Text>
591+
<Text style={{letterSpacing: 6, backgroundColor: '#eeeeee'}}>
592+
[Nested letterSpacing = 6]
593+
</Text>
594+
</Text>
581595
</View>
582596
);
583597
},

RNTester/js/TextInputExample.android.js

+25
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,31 @@ exports.examples = [
567567
);
568568
}
569569
},
570+
{
571+
title: 'letterSpacing',
572+
render: function() {
573+
return (
574+
<View>
575+
<TextInput
576+
style={[styles.singleLine, {letterSpacing: 0}]}
577+
placeholder="letterSpacing = 0"
578+
/>
579+
<TextInput
580+
style={[styles.singleLine, {letterSpacing: 2}]}
581+
placeholder="letterSpacing = 2"
582+
/>
583+
<TextInput
584+
style={[styles.singleLine, {letterSpacing: 9}]}
585+
placeholder="letterSpacing = 9"
586+
/>
587+
<TextInput
588+
style={[styles.singleLine, {letterSpacing: -1}]}
589+
placeholder="letterSpacing = -1"
590+
/>
591+
</View>
592+
);
593+
}
594+
},
570595
{
571596
title: 'Passwords',
572597
render: function() {

ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewProps.java

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public class ViewProps {
8888
public static final String FONT_STYLE = "fontStyle";
8989
public static final String FONT_FAMILY = "fontFamily";
9090
public static final String LINE_HEIGHT = "lineHeight";
91+
public static final String LETTER_SPACING = "letterSpacing";
9192
public static final String NEEDS_OFFSCREEN_ALPHA_COMPOSITING = "needsOffscreenAlphaCompositing";
9293
public static final String NUMBER_OF_LINES = "numberOfLines";
9394
public static final String ELLIPSIZE_MODE = "ellipsizeMode";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
package com.facebook.react.views.text;
11+
12+
import android.annotation.TargetApi;
13+
import android.os.Build;
14+
import android.text.TextPaint;
15+
import android.text.style.MetricAffectingSpan;
16+
17+
import com.facebook.infer.annotation.Assertions;
18+
19+
/**
20+
* A {@link MetricAffectingSpan} that allows to set the letter spacing
21+
* on the selected text span.
22+
*
23+
* The letter spacing is specified in pixels, which are converted to
24+
* ems at paint time; this span must therefore be applied after any
25+
* spans affecting font size.
26+
*/
27+
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
28+
public class CustomLetterSpacingSpan extends MetricAffectingSpan {
29+
30+
private final float mLetterSpacing;
31+
32+
public CustomLetterSpacingSpan(float letterSpacing) {
33+
mLetterSpacing = letterSpacing;
34+
}
35+
36+
@Override
37+
public void updateDrawState(TextPaint paint) {
38+
apply(paint);
39+
}
40+
41+
@Override
42+
public void updateMeasureState(TextPaint paint) {
43+
apply(paint);
44+
}
45+
46+
private void apply(TextPaint paint) {
47+
// mLetterSpacing and paint.getTextSize() are both in pixels,
48+
// yielding an accurate em value.
49+
if (!Float.isNaN(mLetterSpacing)) {
50+
paint.setLetterSpacing(mLetterSpacing / paint.getTextSize());
51+
}
52+
}
53+
}

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

+20
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ private static void buildSpannedFromShadowNode(
118118
new SetSpanOperation(
119119
start, end, new BackgroundColorSpan(textShadowNode.mBackgroundColor)));
120120
}
121+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
122+
if (textShadowNode.mLetterSpacing != Float.NaN) {
123+
ops.add(new SetSpanOperation(
124+
start,
125+
end,
126+
new CustomLetterSpacingSpan(textShadowNode.mLetterSpacing)));
127+
}
128+
}
121129
if (textShadowNode.mFontSize != UNSET) {
122130
ops.add(new SetSpanOperation(start, end, new AbsoluteSizeSpan(textShadowNode.mFontSize)));
123131
}
@@ -228,6 +236,7 @@ private static int parseNumericFontWeight(String fontWeightString) {
228236
}
229237

230238
protected float mLineHeight = Float.NaN;
239+
protected float mLetterSpacing = Float.NaN;
231240
protected boolean mIsColorSet = false;
232241
protected boolean mAllowFontScaling = true;
233242
protected int mColor;
@@ -238,6 +247,7 @@ private static int parseNumericFontWeight(String fontWeightString) {
238247
protected int mFontSize = UNSET;
239248
protected float mFontSizeInput = UNSET;
240249
protected float mLineHeightInput = UNSET;
250+
protected float mLetterSpacingInput = Float.NaN;
241251
protected int mTextAlign = Gravity.NO_GRAVITY;
242252
protected int mTextBreakStrategy =
243253
(Build.VERSION.SDK_INT < Build.VERSION_CODES.M) ? 0 : Layout.BREAK_STRATEGY_HIGH_QUALITY;
@@ -356,12 +366,22 @@ public void setLineHeight(float lineHeight) {
356366
markUpdated();
357367
}
358368

369+
@ReactProp(name = ViewProps.LETTER_SPACING, defaultFloat = Float.NaN)
370+
public void setLetterSpacing(float letterSpacing) {
371+
mLetterSpacingInput = letterSpacing;
372+
mLetterSpacing = mAllowFontScaling
373+
? PixelUtil.toPixelFromSP(mLetterSpacingInput)
374+
: PixelUtil.toPixelFromDIP(mLetterSpacingInput);
375+
markUpdated();
376+
}
377+
359378
@ReactProp(name = ViewProps.ALLOW_FONT_SCALING, defaultBoolean = true)
360379
public void setAllowFontScaling(boolean allowFontScaling) {
361380
if (allowFontScaling != mAllowFontScaling) {
362381
mAllowFontScaling = allowFontScaling;
363382
setFontSize(mFontSizeInput);
364383
setLineHeight(mLineHeightInput);
384+
setLetterSpacing(mLetterSpacingInput);
365385
markUpdated();
366386
}
367387
}

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java

+25
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import android.widget.EditText;
3434
import com.facebook.infer.annotation.Assertions;
3535
import com.facebook.react.bridge.ReactContext;
36+
import com.facebook.react.uimanager.PixelUtil;
3637
import com.facebook.react.uimanager.UIManagerModule;
3738
import com.facebook.react.views.text.CustomStyleSpan;
3839
import com.facebook.react.views.text.ReactTagSpan;
@@ -80,6 +81,7 @@ public class ReactEditText extends EditText {
8081
private @Nullable ScrollWatcher mScrollWatcher;
8182
private final InternalKeyListener mKeyListener;
8283
private boolean mDetectScrollMovement = false;
84+
private float mLetterSpacingPt = 0;
8385

8486
private ReactViewBackgroundManager mReactBackgroundManager;
8587

@@ -627,6 +629,29 @@ public void setBorderStyle(@Nullable String style) {
627629
mReactBackgroundManager.setBorderStyle(style);
628630
}
629631

632+
public void setLetterSpacingPt(float letterSpacingPt) {
633+
mLetterSpacingPt = letterSpacingPt;
634+
updateLetterSpacing();
635+
}
636+
637+
@Override
638+
public void setTextSize (float size) {
639+
super.setTextSize(size);
640+
updateLetterSpacing();
641+
}
642+
643+
@Override
644+
public void setTextSize (int unit, float size) {
645+
super.setTextSize(unit, size);
646+
updateLetterSpacing();
647+
}
648+
649+
protected void updateLetterSpacing() {
650+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
651+
setLetterSpacing(PixelUtil.toPixelFromSP(mLetterSpacingPt) / getTextSize());
652+
}
653+
}
654+
630655
/**
631656
* This class will redirect *TextChanged calls to the listeners only in the case where the text
632657
* is changed by the user, and not explicitly set by JS.

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java

+8
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,14 @@ public void setOnScroll(final ReactEditText view, boolean onScroll) {
306306
}
307307
}
308308

309+
// Sets the letter spacing as an absolute point size.
310+
// This extra handling, on top of what ReactBaseTextShadowNode already does, is required for the
311+
// correct display of spacing in placeholder (hint) text.
312+
@ReactProp(name = ViewProps.LETTER_SPACING, defaultFloat = 0)
313+
public void setLetterSpacing(ReactEditText view, float letterSpacing) {
314+
view.setLetterSpacingPt(letterSpacing);
315+
}
316+
309317
@ReactProp(name = "placeholder")
310318
public void setPlaceholder(ReactEditText view, @Nullable String placeholder) {
311319
view.setHint(placeholder);

0 commit comments

Comments
 (0)