Is it possible to change width of button according to text inside it. #924
Replies: 3 comments
-
The font object has a |
Beta Was this translation helpful? Give feedback.
-
Expanding William's response you can also use it within a computed value like this const progress = useLoop()
const font = useFont("any font", 20)
const textWidth = useComputedValue(() => {
return font.getTextWidth(`${Math.floor(progress.current * 100)}%`)
}, [progress, font]) This one is a simple example of getting the text width for percentage from "0%" to "100%" |
Beta Was this translation helpful? Give feedback.
-
Hi, Thanks |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I am new to React Native and Skia.
I want to understand is it possible to change width of a button according to text inside it.
Below is the code snippet
`import { SafeAreaView, View, StyleSheet, useWindowDimensions, TouchableOpacity } from "react-native"
import {
Canvas, Box, BoxShadow, Fill, rrect, rect, Text, useFont
} from "@shopify/react-native-skia";
import React, { useState } from 'react';
const ButtonText = () => {
const fontSize = 22;
const font = useFont(require("../assets/Dosis-VariableFont_wght.ttf"), fontSize);
if (font === null) {
return null;
}
return (
<Canvas style={{ width: "100%", height: "100%" }}>
<Box box={rrect(rect(30, 50, 95, 64), 15, 15)} color="white">
)
}
export default ButtonText
`
Here is the output :
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions