codequest
asked on
Calculate Text Length in Pixels
I'm going to dynamically build a custom control, and add it to a page. I'm not sure which kind of control elements yet, because:
After I add the text to the control's text element,
I need a way to calculate the length of the text in the text element...
So I can then change the element's height, top and width.....
So that it only shows two lines (wrapped), and that it's bottom edge is at "set-able" position.
Any suggestions on how to calculate the text width of any appropriate text element would be welcome.
Thanks!
After I add the text to the control's text element,
I need a way to calculate the length of the text in the text element...
So I can then change the element's height, top and width.....
So that it only shows two lines (wrapped), and that it's bottom edge is at "set-able" position.
Any suggestions on how to calculate the text width of any appropriate text element would be welcome.
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
There are no variables. There is a method in Graphics object to actually calculate size of the string. And that will give you the height and width of the string in pixels. I don't remeber the name of the method at the momemt. Look in the doucmentatio
ASKER
Ok, cool. I'll check it out. Thanks!
ASKER
I see the Bitmap.width property, though it seems that it's necessary to set the width (either by loading something into it, or just specifying it when new) before it can be DrawString'd into...and then the width is just what it was initially set to.
So while I can get the width, I'm not seeing how the DrawString method is going to change the width.
There is definitely a method in windows forms for calculating the height and width of a rendered string (I think it's called MeasureRendered, or something like that), but I don't see its equivalent in web forms.
So while I can get the width, I'm not seeing how the DrawString method is going to change the width.
There is definitely a method in windows forms for calculating the height and width of a rendered string (I think it's called MeasureRendered, or something like that), but I don't see its equivalent in web forms.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for inputs. Those are great tips. I'll check them out in the AM...they should get me there, at least to test this approach.
I appreciate the uncertainty about font size...in the business context for this app there should some control over how the page is display (but you never know...)
I appreciate the uncertainty about font size...in the business context for this app there should some control over how the page is display (but you never know...)
ASKER
Works great. Thanks!
ASKER
I recently learned how to create graphic objects, so that's a start.
If I don't like the overhead, then the fallback will be to use the method you suggest to develop some conversion values for the target fonts and font sizes, using an assortment of random phrases. Then I can apply the conversion value to the character count and come up with an approximate pixel length. Fortunately, the app is somewhat forgiving if the length is miscalculated (some truncation, and some un-used double lines, will be acceptable.)
One big help would be if you could indicate the variables that I need to use to get the size of the string from the bitmap object. I could probably work backwards from those variables to build the routines.
In the mean time I'll do some experimentation to find those variables.