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

2/n Nested Text textAlignVertical (Android) - adding textAlignVertical prop to NestedText #35704

Closed
wants to merge 65 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
c683779
draft example to add superscript to Text
fabOnReact Dec 22, 2022
6716e0f
cpp TextAttributes settings crash RNTester
fabOnReact Dec 22, 2022
7d147de
passing textAlignVert from js NestedText to Java
fabOnReact Dec 22, 2022
7914cfd
Verify configs behind mIsAccessibilityLink or check corresponding con…
fabOnReact Dec 26, 2022
fdeb37c
Modify Superscript implementation to align to the top (stackoverflow-…
fabOnReact Dec 26, 2022
5565bc1
adapt Superscript to top/bottom
fabOnReact Dec 27, 2022
9de465b
Update TopAlignSpan baseline position from ReactTextView#onLayout wit…
fabOnReact Dec 28, 2022
7ab97c2
Add logic to detect parent textAlignVertical and correctly shift base…
fabOnReact Dec 28, 2022
0f7d69d
delete ReactBottomAlignSpan
fabOnReact Dec 28, 2022
59fe775
Avoid adding ReactAlignSpan to span of the parent TextView
fabOnReact Dec 29, 2022
5017156
The calculatedHeight used to compute the margin is an aproximate valu…
fabOnReact Jan 2, 2023
f75e899
Nested Text correctly aligned multiline spans (different positions an…
fabOnReact Jan 2, 2023
ba18167
refactor algo
fabOnReact Jan 2, 2023
9a7ad5b
Text correctly aligns with nested inline image. The height of the inl…
fabOnReact Jan 4, 2023
61e5089
Use lineHeight to position span textAlignVertical
fabOnReact Jan 5, 2023
582faa4
Parent component does not have align property
fabOnReact Jan 5, 2023
8573fd5
adding different test scenarios
fabOnReact Jan 5, 2023
28608f1
Bottom and center alignment
fabOnReact Jan 5, 2023
957c784
minor changes
fabOnReact Jan 5, 2023
ba5223b
Changing text align from center to top results in mHighestLineHeight …
fabOnReact Jan 10, 2023
47ddd11
Text does not align correctly without lineHeight prop (video, stackov…
fabOnReact Jan 10, 2023
ce2b8eb
fix bottom alignment when lineHeight prop provided
fabOnReact Jan 10, 2023
87d205f
remove not relevant code
fabOnReact Jan 10, 2023
7ac7a9a
move ReactAlignSpan to CustomStyleSpan
fabOnReact Jan 11, 2023
bec2e1d
adding comments to baselineShift logic
fabOnReact Jan 11, 2023
2ff855b
update comments
fabOnReact Jan 11, 2023
ee66a98
reapply relevant changes from PR#7
fabOnReact Jan 11, 2023
b3efb82
Merge branch 'main' into nested-text-superscript
fabOnReact Jan 12, 2023
395bbd2
Move ReactAbsSizeSpan to separate PR and organize task in separate table
fabOnReact Jan 12, 2023
af8a7e9
Calculate font top and bottom with different fontSizes
fabOnReact Jan 13, 2023
e363196
avoid using TextBounds in AbsoluteSpan
fabOnReact Jan 13, 2023
dbe25d3
adding constructor to CustomStyleSpan and AbsoluteSpan
fabOnReact Jan 13, 2023
812d406
Combining lineHeight and fontSize can cause not correct top/bottom al…
fabOnReact Jan 13, 2023
4f6095e
Initial position is not correct (screenshot). 1 line is added to base…
fabOnReact Jan 13, 2023
49abefd
Avoid removing ascent or descent when lineHeight is higher then fontM…
fabOnReact Jan 16, 2023
469c6d7
Fixing issue with ascent and descent when aligning text with lineHeight
fabOnReact Jan 16, 2023
003ab18
Fix flow errors (link1, link2, link3)
fabOnReact Jan 17, 2023
17c70ad
Fix flow errors (link1, link2, link3, commit)
fabOnReact Jan 17, 2023
7fca70c
Run a loop over absoluteSizeSpans and check that any of them has text…
fabOnReact Jan 17, 2023
6c4e19b
removing not required prop in ViewProps.cpp
fabOnReact Jan 17, 2023
a2e3d4e
remove Nullable from ReactAbsSizeSpan constructor
fabOnReact Jan 17, 2023
5b9c505
Merge branch 'main' into nested-text-superscript
fabOnReact Jan 17, 2023
72f4726
remove changes to TextMeasureCache.h
fabOnReact Jan 17, 2023
331d945
default std::string constructor inits empty strig
fabOnReact Jan 23, 2023
e85bbe7
Merge branch 'main' into nested-text-superscript
fabOnReact Jan 31, 2023
1230e06
Move the logic for shifting text based on the lineheight to CustomSty…
fabOnReact Feb 1, 2023
c139885
Add logic to align span with smaller fontSize
fabOnReact Feb 1, 2023
5879630
remove ReactAbsoluteSizeSpan
fabOnReact Feb 1, 2023
c4d82d3
remove currentText (debug)
fabOnReact Feb 1, 2023
eb0b424
update PR based on https://github.com/facebook/react-native/pull/35949
fabOnReact Feb 9, 2023
112eeee
The parent Text does not align correctly with CustomStyleSpan with Gr…
fabOnReact Feb 9, 2023
5f76bff
removing example
fabOnReact Feb 10, 2023
a770ea8
Merge branch 'main' into nested-text-superscript
fabOnReact Feb 10, 2023
95324ec
Merge branch 'main' into nested-text-superscript
fabOnReact Feb 20, 2023
e59529d
update PR based on https://github.com/facebook/react-native/pull/35949
fabOnReact Feb 20, 2023
224c58a
update example
fabOnReact Feb 21, 2023
c9278d9
Change internal span attributes
fabOnReact Feb 22, 2023
94d644e
update comments
fabOnReact Feb 22, 2023
0e20043
update comments
fabOnReact Feb 22, 2023
d281638
update comments
fabOnReact Feb 23, 2023
46e3815
avoid change alignment with updateMeasureState
fabOnReact Feb 23, 2023
c91e24d
remove not relevant code
fabOnReact Feb 24, 2023
89b95dc
remove not relevant code
fabOnReact Feb 24, 2023
0bf75e7
update example
fabOnReact Feb 24, 2023
dbeecaa
Merge branch 'main' into nested-text-superscript
fabOnReact Feb 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add logic to detect parent textAlignVertical and correctly shift base…
  • Loading branch information
fabOnReact committed Dec 28, 2022
commit 7ab97c2188ce371fbfc47ebf53fa8f8e0d5c45e5
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.views.text;

import android.text.TextPaint;
import android.text.style.SuperscriptSpan;
import android.view.Gravity;
import com.facebook.common.logging.FLog;

/** ratio 0 for center ratio 0.4 for top ratio */
public class ReactAlignSpan extends SuperscriptSpan implements ReactSpan {
private static final String TAG = "ReactTopAlignSpan";
private Integer mParentHeight;
private String mTextAlignVertical;
private String mParentTextAlignVertical;
private Integer mParentGravity;

ReactAlignSpan(String textAlignVertical) {
mTextAlignVertical = textAlignVertical;
}

private double convertTextAlignToStep(String textAlign) {
switch (textAlign) {
case "top-child":
return 1;
case "center-child":
return 0.5;
case "bottom-child":
return 0;
}
FLog.w(
TAG,
"unable to convert textAlign: "
+ textAlign
+ " to Integer (step) in ReactTopAlignSpan method convertTextAlignToStep.");
// improve this
return -1;
}

private double convertGravityToStep(Integer gravity) {
if (gravity.equals(Gravity.TOP) || gravity.equals(Gravity.TOP | Gravity.START)) {
return 1;
} else if (gravity.equals(Gravity.CENTER_VERTICAL)
|| gravity.equals(Gravity.CENTER_VERTICAL | Gravity.START)) {
return 0.5;
} else if (gravity.equals(Gravity.BOTTOM) || gravity.equals(Gravity.BOTTOM | Gravity.START)) {
return 0;
}
// update it
FLog.w(
TAG,
"unable to convert gravity: "
+ gravity
+ " to Integer (step) in ReactTopAlignSpan method convertTextAlignToStep.");
// improve this
return -1;
}

@Override
public void updateDrawState(TextPaint ds) {
if (mTextAlignVertical == null || mParentGravity == null) {
return;
}
double convertedTextAlignVertical = convertTextAlignToStep(mTextAlignVertical);
double convertedParentTextAlignVertical = convertGravityToStep(mParentGravity);
if (convertedTextAlignVertical == -1 || convertedParentTextAlignVertical == -1) {
return;
}
double numberOfSteps = convertedTextAlignVertical - convertedParentTextAlignVertical;

if (mParentHeight != null) {
if (numberOfSteps >= 0) {
ds.baselineShift -= (mParentHeight - ds.getTextSize()) * numberOfSteps;
} else {
ds.baselineShift -= (mParentHeight - ds.getTextSize() - ds.descent()) * numberOfSteps;
}
}
}

public void updateSpan(Integer height, int gravity) {
mParentHeight = height;
mParentGravity = gravity;
}

@Override
public void updateMeasureState(TextPaint tp) {
updateDrawState(tp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ protected void onLayout(
// TODO T62882314: Delete this method when Fabric is fully released in OSS
if (getText() instanceof Spanned) {
Spanned text = (Spanned) getText();
ReactTopAlignSpan[] spans = text.getSpans(0, text.length(), ReactTopAlignSpan.class);
ReactAlignSpan[] spans = text.getSpans(0, text.length(), ReactAlignSpan.class);
if (spans.length != 0) {
for (ReactTopAlignSpan span : spans) {
span.setHeight(getHeight());
for (ReactAlignSpan span : spans) {
span.updateSpan(getHeight(), getGravity());
invalidate();
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,11 @@ private static void buildSpannableFromFragment(
new TextInlineViewPlaceholderSpan(reactTag, (int) width, (int) height)));
} else if (end >= start) {
if (textAttributes.mVerticalAlign != null
&& textAttributes.mVerticalAlign.equals("top-child")) {
ops.add(new SetSpanOperation(start, end, new ReactTopAlignSpan()));
}
if (textAttributes.mVerticalAlign != null
&& textAttributes.mVerticalAlign.equals("bottom-child")) {
ops.add(new SetSpanOperation(start, end, new ReactBottomAlignSpan()));
&& (textAttributes.mVerticalAlign.equals("top-child")
|| textAttributes.mVerticalAlign.equals("bottom-child"))) {
ops.add(
new SetSpanOperation(
start, end, new ReactAlignSpan(textAttributes.mVerticalAlign)));
}
if (textAttributes.mIsAccessibilityLink) {
ops.add(new SetSpanOperation(start, end, new ReactClickableSpan(reactTag)));
Expand Down