score:30
Accepted answer
Please check this. is a solution using canvas
function get_tex_width(txt, font) {
this.element = document.createElement('canvas');
this.context = this.element.getContext("2d");
this.context.font = font;
return this.context.measureText(txt).width;
}
alert('Calculated width ' + get_tex_width("Hello World", "30px Arial"));
alert("Span text width "+$("span").width());
EDIT
The solution using canvas is not the best, each browser deal different canvas size.
Here is a nice solution to get size of text using a temporary element. Demo
EDIT
The canvas spec doesn't give us a method for measuring the height of a string, so for this we can use parseInt(context.font)
.
TO get width and height. This trick work only with px size.
function get_tex_size(txt, font) {
this.element = document.createElement('canvas');
this.context = this.element.getContext("2d");
this.context.font = font;
var tsize = {'width':this.context.measureText(txt).width, 'height':parseInt(this.context.font)};
return tsize;
}
var tsize = get_tex_size("Hello World", "30px Arial");
alert('Calculated width ' + tsize['width'] + '; Calculated height ' + tsize['height']);
Source: stackoverflow.com
Related Query
- Measuring text width/height without rendering
- Full height & width without scrollbar in Material UI React app
- Calculate pixel width of text without knowing font in React/JavaScript
- How to change the Swiper height or slide width in React JS without using fixed CSS
- Summernote rich text editor-restrict image above particular height and width
- Adding text over image in ReactJS by relative height and width
- How to get an image's width and height after scaling & rendering the react component
- Tooltip display on top without affecting the height and width of container
- react rendering text without new line
- Cloudinary React component not rendering width or height transformation to URL
- Text exceeding maximum height and width of the container
- Can I achieve css rectangle shapes without width and height property
- React-native view auto width by text inside
- How to set image width to be 100% and height to be auto in react native?
- Is it possible to use React without rendering HTML?
- How to output text in ReactJS without wrapping it in span
- Set height and width for responsive chart using recharts ( Barchart )
- Image elements do not have explicit width and height
- React Native: How to set <TextInput/>'s height and width to <View/> container?
- React - Get text of div on click without refs
- How to mirror and resize the part of image with fixed width and height
- How to use jinja2 server side rendering alongside react without violating inline-script CSP
- Material-UI DataGrid dynamic row height based on text content
- React Bootstrap adjust width of text inputs
- React Router v4 - Pre-Mount Component Without Rendering
- URL change without re rendering in React router
- Rendering ContentEditable component without getting ContentEditableWarning?
- Rendering a Google Map without react-google-map
- How to make ReactPlayer scale with height and width
- Possible to have a dynamically height adjusted textarea without constant reflows?
More Query from same tag
- Styling React Native Picker
- Get values from useEffect hook and display in React's functional components
- How to filter the aray?
- How to deal with the dependency between two async thunks
- I'm trying to insert some HTML into jodit-react editor on my cursor position, it somehow inserts but not exactly at my cursor position
- Failed to load resources error 404 while deploying create-react-app
- React htmlFor - clicking label focus on the wrong input
- Uncaught TypeError: Cannot read property '_props' of undefined
- How to display a react-bootstrap modal from another component
- Return from a funtion only when API returns a success response from backend in React
- React custom change event handler in form hook
- How to sum props sent by a root component
- How to pass function as prop in react routerv5 with the Link tag
- How to pass two parameters in the 'onSave ()' function in react-editext?
- how to render visible array in react js?
- How to add buttons in material-ui data grid rows?
- Problem with request in react and express.js
- React Material Table action OnClick event for edit and delete does not fire action
- Response is not assignable to type Promise<any> Property finally is missing?
- Semantic UI Dropdown component max-width in ReactJS
- Sinon anonymous stub passed as a parameter
- React - useState doesn't update state on setInterval
- How to add items which contains in loop and adding into the array?
- Is it a correct approach to pass useState setter to a child for updating parents state?
- Set timeout issue with promise (react+redux)
- useState not returning data from fetch request
- How to handler onClick on child component in parent component
- Material UI: how do I use Theme values with withStyles?
- Dark theme / with hooks
- How to handle multiple websocket subscriptions in React?