I'm trying to figure out if I can measure a string height in javascript. I have this code in C#, but I would like to see if I can get this in Javascript as well. I've looked through past SO's and found this as an answer (don't have link):
function get_tex_height(txt, font) {
this.element = document.createElement('canvas');
this.context = this.element.getContext('2d');
this.context.font = font;
return this.context.measureText(txt).height;
}
For whatever reason, this isn't working. Why? you might ask. I have no idea. and I have no way of knowing either. I am rednering HTML in a mobile app in a UIWebView
which the only method of any kind of debugging I have is alert(somethinghere)
. If the JS doesn't work, then it just doesn't continue executing code and I have no idea where it failed.
So I want to just see if I can pass it a font, font size, width constraint and get an accurate height of what this string will be when rendered. If this is possible, which I'm starting to conclude is not. I might just have to use my C# method for this.