Skip to content

Commit b0668b9

Browse files
committed
Clode cleanup + Remove FontSize constants (android)
1 parent c4534c7 commit b0668b9

File tree

3 files changed

+27
-32
lines changed

3 files changed

+27
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
77
- Code cleanup, minor enhancements.
88

99
### Removed
10-
- iOS: Removed the `FontSize` constants.
10+
- Removed the `FontSize` constants.
1111

1212
## [2.0.0-beta.6] - 2018-08-21
1313

README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,30 +70,32 @@ See [Manual Installation](https://github.com/aMarCruz/react-native-text-size/wik
7070
measure(options: TSMeasureParams): Promise<TSMeasureResult>
7171
```
7272

73-
This function measures the text like RN does when the text does not have embedded images or text with different sizes. It take a subset of the properties used by [`<Text>`](https://facebook.github.io/react-native/docs/text) to describe the font to use.
73+
This function measures the text as RN does and its result is consistent\* with that of `Text`'s onLayout event. It take a subset of the properties used by [`<Text>`](https://facebook.github.io/react-native/docs/text#props) to describe the font and other options to use.
7474

75-
If you provide the `width`, the measurement will apply the restriction and take into account the automatic line breaks, in addition to the explicit ones.
75+
If you provide the `width`, the measurement will apply automatic wrapping in addition to the explicit line breaks.
76+
77+
\* _On iOS, RN takes into account the absolute position on the screen to calculate the dimensions. rnTextSize can't do that but adds 1 pixel to both width and height avoid overflow._
7678

7779
**NOTE:**
7880

79-
Although this function is accurate and provides complete information, it can be heavy if the text is a lot, like the one that can be displayed in a FlatList. For these cases, it is better to use `flatHeights` which is much faster.
81+
Although this function is accurate and provides complete information, it can be heavy if the text is a lot, like the one that can be displayed in a FlatList. For these cases, it is better to use [`flatHeights`](#flatheights), which is optimized fotr those cases.
8082

8183
<a name="tsmeasureparams"></a>**TSMeasureParams**
8284

83-
JS object with the text to measure, the maximum width, and properties like ones in the react-native [`<Text>`](https://facebook.github.io/react-native/docs/text) component.
85+
Plain JS object with this properties:
8486

8587
Property | Type | Default | Notes
8688
---------- | ------ | -------- | ------
8789
text | string | (none) | This is the only required parameter and may include _emojis_ or be empty, but it can not be `null`. If this is an empty string the resulting `width` will be zero.
8890
width | number | Infinity | Restrict the width. The resulting height will vary depending on the automatic flow of the text.
89-
usePreciseWidth | boolean | false | If `true`, request an exact `width` and the value of `lastWidth`. Used only in Android, iOS always returns both.<br>You can see the effect of this flag in the [sample App](https://github.com/aMarCruz/react-native-text-size/tree/master/example).
90-
fontFamily | string | OS dependent | The default is the same applied by React Native: Roboto in Android, San Francisco in iOS.<br>Note: Device manufacturer or custom ROM can change this
91+
usePreciseWidth | boolean | false | If `true`, request an exact `width` and the value of `lastLineWidth` (iOS always returns the exact width).<br>You can see the effect of this flag in the [sample App](https://github.com/aMarCruz/react-native-text-size/tree/master/example).
92+
fontFamily | string | OS dependent | The default is the same applied by React Native: Roboto in Android, San Francisco in iOS.<br>**Note:** Device manufacturer or custom ROM can change the default font.
9193
fontWeight | string | 'normal' | On android, numeric ranges has no granularity and '500' to '900' becomes 'bold', but you can use fonts of specific weights, like "sans-serif-medium".
92-
fontSize | number | 14 | The default value is that used by RN and is provided in the `.FontSize.default` constant.
94+
fontSize | number | 14 | The default value comes from RN.
9395
fontStyle | string | 'normal' | One of "normal" or "italic".
9496
fontVariant | array | (none) | _iOS only_
9597
allowFontScaling | boolean | true | To respect the user' setting of large fonts (i.e. use SP units).
96-
letterSpacing | number | (none) | Additional spacing between characters (a.k.a. `tracking`).<br>NOTE: In iOS a zero cancels automatic kerning.<br>_All iOS, Android with API 21+ and RN 0.55+_
98+
letterSpacing | number | (none) | Additional spacing between characters (a.k.a. `tracking`).<br>**Note:** In iOS a zero cancels automatic kerning.<br>_All iOS, Android with API 21+ and RN 0.55+_
9799
includeFontPadding | boolean | true | Include additional top and bottom padding, to avoid clipping certain characters.<br>_Android only_
98100
textBreakStrategy | string | 'highQuality' | One of 'simple', 'balanced', or 'highQuality'.<br>_Android only, with API 23+_
99101

@@ -105,7 +107,7 @@ The [sample App](https://github.com/aMarCruz/react-native-text-size/tree/master/
105107

106108
Property | Type | Notes
107109
--------- | ------ | ------
108-
width | number | Total used width. It may be less or equal to the given width and, in Andoid, its value may vary depending on the `usePreciseWidth` flag.
110+
width | number | Total used width. It may be less or equal to the given width. On Android this value may vary depending on the `usePreciseWidth` flag.
109111
height | number | Total height, including top and bottom padding if `includingFontPadding` was set (the default).
110112
lastLineWidth | number | Width of the last line, without trailing blanks.<br>__Note:__ If `usePreciseWidth` is `false` (the default), this field is undefined.
111113
lineCount | number | Number of lines, taking into account hard and automatic line breaks.
@@ -191,7 +193,6 @@ I did tests on 5,000 random text blocks and these were the results (ms):
191193
Android | 49,624 | 1,091
192194
iOS | 1,949 | 732
193195

194-
195196
In the future I will prepare an example of its use with FlatList and multiple styles on the same card.
196197

197198
**TSHeightsParams**
@@ -204,7 +205,7 @@ This is an object similar to the one received by `measure`, but the `text` prope
204205

205206
Property | Type | Default
206207
---------- | ------ | --------
207-
text | string | (none)
208+
text | string[] | (none)
208209
width | number | Infinity
209210
fontFamily | string | OS dependent
210211
fontWeight | string | 'normal'
@@ -218,6 +219,8 @@ textBreakStrategy | string | 'highQuality'
218219

219220
The result is a Promise that resolves to an array with the height of each block (_SP_), in the same order in which they were received.
220221

222+
Unlike measure, `null` elements returns 0 without generating error, and empty strings returns the same height that RN assigns to empty `<Text>` components.
223+
221224
---
222225

223226
## `specsForTextStyles`
@@ -294,7 +297,7 @@ leading | number | The recommended additional space to add between lines of
294297
lineHeight | number | The recommended line height (remember, _SP_ in Android). It should be greater if text contain Unicode symbols, such as emojis.
295298
_hash | number | Hash code, maybe useful for debugging.
296299

297-
> \* Using floats is more accurate than integers and allows you to use your preferred rounding method, but consider no more than 5 digits of precision in this values. Also, remember RN doesn't work with subpixels in Android and will truncate this values.
300+
\* _Using floats is more accurate than integers and allows you to use your preferred rounding method, but consider no more than 5 digits of precision in this values. Also, remember RN doesn't work with subpixels in Android and will truncate this values._
298301

299302
See more in:
300303

android/src/main/java/com/github/amarcruz/rntextsize/RNTextSizeModule.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,6 @@ public String getName() {
5858
return TAG;
5959
}
6060

61-
@Override
62-
public Map<String, Object> getConstants() {
63-
final Map<String, Object> constants = new HashMap<>();
64-
final Map<String, Object> fontSizes = new HashMap<>();
65-
66-
fontSizes.put("default", RNTextSizeConf.getDefaultFontSize());
67-
constants.put("FontSize", fontSizes);
68-
return constants;
69-
}
70-
7161
/**
7262
* Based on ReactTextShadowNode.java
7363
*/
@@ -91,11 +81,8 @@ public void measure(@Nullable final ReadableMap specs, final Promise promise) {
9181

9282
final WritableMap result = Arguments.createMap();
9383
if (_text.isEmpty()) {
94-
// RN 0.56 consistently sets the height at 14dp divided by the density
95-
// plus 1 if includeFontPadding when text is empty, so we do the same.
96-
float height = (14.0f / density) + (includeFontPadding ? 1 : 0);
9784
result.putInt("width", 0);
98-
result.putDouble("height", height);
85+
result.putDouble("height", minimalHeight(density, includeFontPadding));
9986
result.putInt("lastLineWidth", 0);
10087
result.putInt("lineCount", 0);
10188
promise.resolve(result);
@@ -208,7 +195,6 @@ public void flatHeights(@Nullable final ReadableMap specs, final Promise promise
208195
RNTextSizeSpannedText.spannedFromSpecsAndText(mReactContext, conf, sb);
209196

210197
final TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
211-
double minHeight = Double.NaN;
212198
Layout layout;
213199
try {
214200

@@ -221,10 +207,7 @@ public void flatHeights(@Nullable final ReadableMap specs, final Promise promise
221207

222208
final String text = texts.getString(ix);
223209
if (text.isEmpty()) {
224-
if (Double.isNaN(minHeight)) {
225-
minHeight = (14.0f / density) + (includeFontPadding ? 1 : 0);
226-
}
227-
result.pushDouble(minHeight);
210+
result.pushDouble(minimalHeight(density, includeFontPadding);
228211
continue;
229212
}
230213

@@ -371,6 +354,15 @@ private RNTextSizeConf getConf(final ReadableMap specs, final Promise promise) {
371354
return getConf(specs, promise, false);
372355
}
373356

357+
/**
358+
* RN consistently sets the height at 14dp divided by the density
359+
* plus 1 if includeFontPadding when text is empty, so we do the same.
360+
*/
361+
private double minimalHeight(final float density, final boolean includeFontPadding) {
362+
final double height = 14.0 / density;
363+
return includeFontPadding ? height + 1.0 : height;
364+
}
365+
374366
/**
375367
* This is for 'fontFromFontStyle', makes the minimal info required.
376368
* @param suffix The font variant

0 commit comments

Comments
 (0)