forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Setting the line height with the help of Android-provided StaticLayout is incorrect. A simple example app will display the following when `setLineSpacing(50.f, 0.f)` is set: {F62987699}. You'll notice that the height of the first line is a few pixels shorter than the other lines. So we use a custom LineHeightSpan instead, which needs to be applied to the text itself, and no height-related attributes need to be set on the TextView itself. Reviewed By: lexs Differential Revision: D3841658 fbshipit-source-id: 7257df4f1b2ce037554c7a7a5ca8f547a2056939
- Loading branch information
1 parent
16bdbee
commit c79f617
Showing
5 changed files
with
66 additions
and
35 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
ReactAndroid/src/main/java/com/facebook/react/views/text/CustomLineHeightSpan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
package com.facebook.react.views.text; | ||
|
||
import android.graphics.Paint; | ||
import android.text.style.LineHeightSpan; | ||
|
||
/** | ||
* We use a custom {@link LineHeightSpan}, because `lineSpacingExtra` is broken. Details here: | ||
* https://github.com/facebook/react-native/issues/7546 | ||
*/ | ||
public class CustomLineHeightSpan implements LineHeightSpan { | ||
private final int mHeight; | ||
|
||
CustomLineHeightSpan(float height) { | ||
this.mHeight = (int) Math.ceil(height); | ||
} | ||
|
||
@Override | ||
public void chooseHeight( | ||
CharSequence text, | ||
int start, | ||
int end, | ||
int spanstartv, | ||
int v, | ||
Paint.FontMetricsInt fm) { | ||
// This is more complicated that I wanted it to be. You can find a good explanation of what the | ||
// FontMetrics mean here: http://stackoverflow.com/questions/27631736. | ||
// The general solution is that if there's not enough height to show the full line height, we | ||
// will prioritize in this order: ascent, descent, bottom, top | ||
|
||
if (-fm.ascent > mHeight) { | ||
// Show as much ascent as possible | ||
fm.top = fm.ascent = -mHeight; | ||
fm.bottom = fm.descent = 0; | ||
} else if (-fm.ascent + fm.descent > mHeight) { | ||
// Show all ascent, and as much descent as possible | ||
fm.top = fm.ascent; | ||
fm.bottom = fm.descent = mHeight + fm.ascent; | ||
} else if (-fm.ascent + fm.bottom > mHeight) { | ||
// Show all ascent, descent, as much bottom as possible | ||
fm.top = fm.ascent; | ||
fm.bottom = fm.ascent + mHeight; | ||
} else if (-fm.top + fm.bottom > mHeight) { | ||
// Show all ascent, descent, bottom, as much top as possible | ||
fm.top = fm.bottom - mHeight; | ||
} else { | ||
// Show proportionally additional ascent and top | ||
final int additional = mHeight - (-fm.top + fm.bottom); | ||
fm.top -= additional; | ||
fm.ascent -= additional; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters