@matiks/rn-stroke-text 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -137,41 +137,35 @@ class StrokeTextView(
|
|
|
137
137
|
text
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
false
|
|
148
|
-
)
|
|
140
|
+
// Use StaticLayout.Builder for API 23+
|
|
141
|
+
val builder = StaticLayout.Builder.obtain(displayText, 0, displayText.length, textPaint, safeWidth)
|
|
142
|
+
.setAlignment(alignment)
|
|
143
|
+
.setLineSpacing(0f, 1f)
|
|
144
|
+
.setIncludePad(false)
|
|
145
|
+
|
|
146
|
+
var layout = builder.build()
|
|
149
147
|
|
|
150
148
|
if (numberOfLines > 0 && layout.lineCount > numberOfLines) {
|
|
151
149
|
val end = layout.getLineEnd(numberOfLines - 1)
|
|
152
150
|
displayText = displayText.subSequence(0, end)
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
0f,
|
|
161
|
-
false
|
|
162
|
-
)
|
|
151
|
+
|
|
152
|
+
val truncatedBuilder = StaticLayout.Builder.obtain(displayText, 0, displayText.length, textPaint, safeWidth)
|
|
153
|
+
.setAlignment(alignment)
|
|
154
|
+
.setLineSpacing(0f, 1f)
|
|
155
|
+
.setIncludePad(false)
|
|
156
|
+
|
|
157
|
+
layout = truncatedBuilder.build()
|
|
163
158
|
}
|
|
164
159
|
|
|
165
160
|
textLayout = layout
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
)
|
|
161
|
+
|
|
162
|
+
// For stroke layout, we use the same text
|
|
163
|
+
val strokeBuilder = StaticLayout.Builder.obtain(displayText, 0, displayText.length, strokePaint, safeWidth)
|
|
164
|
+
.setAlignment(alignment)
|
|
165
|
+
.setLineSpacing(0f, 1f)
|
|
166
|
+
.setIncludePad(false)
|
|
167
|
+
|
|
168
|
+
strokeLayout = strokeBuilder.build()
|
|
175
169
|
|
|
176
170
|
layoutDirty = false
|
|
177
171
|
}
|
|
@@ -193,10 +187,10 @@ class StrokeTextView(
|
|
|
193
187
|
|
|
194
188
|
ensureLayout(measureWidth)
|
|
195
189
|
|
|
196
|
-
val
|
|
197
|
-
val
|
|
190
|
+
val wPx = textLayout?.width?.toFloat() ?: 0f
|
|
191
|
+
val hPx = textLayout?.height?.toFloat() ?: 0f
|
|
198
192
|
|
|
199
|
-
return Pair(
|
|
193
|
+
return Pair(pxToDp(wPx).toDouble(), pxToDp(hPx).toDouble())
|
|
200
194
|
}
|
|
201
195
|
|
|
202
196
|
private fun measureTextWidth(): Float {
|
|
@@ -327,6 +321,9 @@ class StrokeTextView(
|
|
|
327
321
|
resources.displayMetrics
|
|
328
322
|
)
|
|
329
323
|
|
|
324
|
+
private fun pxToDp(px: Float): Float =
|
|
325
|
+
px / resources.displayMetrics.density
|
|
326
|
+
|
|
330
327
|
private fun parseColor(color: String): Int =
|
|
331
328
|
try {
|
|
332
329
|
Color.parseColor(color)
|
package/package.json
CHANGED
package/src/index.tsx
CHANGED
|
@@ -15,27 +15,31 @@ interface StrokeTextViewProps extends MatiksStrokeTextProps {
|
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
const StrokeTextView = ({ styles, ...props }: StrokeTextViewProps) => {
|
|
18
|
+
const StrokeTextView = ({ text, styles, ...props }: StrokeTextViewProps) => {
|
|
19
19
|
const ref = useRef<MatiksStrokeText | null>(null);
|
|
20
20
|
const [size, setSize] = useState({ width: 0, height: 0 });
|
|
21
|
+
const [isReady, setIsReady] = useState(false)
|
|
21
22
|
|
|
22
23
|
useEffect(() => {
|
|
23
24
|
if (!ref.current) return;
|
|
24
|
-
|
|
25
25
|
const result = ref.current.measureDimensions();
|
|
26
26
|
setSize(result);
|
|
27
27
|
}, [
|
|
28
28
|
props.fontFamily,
|
|
29
29
|
props.fontSize,
|
|
30
|
-
|
|
30
|
+
text,
|
|
31
|
+
isReady
|
|
31
32
|
]);
|
|
32
33
|
|
|
33
34
|
return (
|
|
34
35
|
<StrokeTextViewComponent
|
|
35
36
|
{...props}
|
|
37
|
+
text={String(text)}
|
|
36
38
|
style={[{ width: size.width, height: size.height }, styles]}
|
|
37
39
|
hybridRef={callback((_ref) => {
|
|
40
|
+
if(ref.current) return
|
|
38
41
|
ref.current = _ref;
|
|
42
|
+
setIsReady(true)
|
|
39
43
|
})}
|
|
40
44
|
/>
|
|
41
45
|
);
|