string resizing

  • Thread starter Thread starter Matt MacDonald
  • Start date Start date
M

Matt MacDonald

Here's a pretty easy one i think.

How do I trim a string to a certain (physical) length i.e. in pixels, as
opposed to characters?

I'm tring to fit strings in data cells in an asp datagrid.

Thanks alot,
Matt
 
Matt MacDonald said:
Here's a pretty easy one i think.

How do I trim a string to a certain (physical) length i.e. in pixels, as
opposed to characters?

I'm tring to fit strings in data cells in an asp datagrid.

Thanks alot,
Matt

Not so easy: what is the EXACT font that is used by the browser,
what is the EXACT size that is used there?

Within the System.Drawing namespace there is a Graphics.MeasureString method,
you could try to use that: keep trimming the string until the size is below your
maximum. This would be very slow.

Maybe you can use css for this and let the browser handle it :
search for "wrap" and "overflow".

Hans Kesting
 
It's not so easy. First you have to know what font is being used in
the browser. Then you need a table of character widths in pixels.
Finally you need to write a routine to index into the table and add up
all the widths for each character you want to display. You need to
know any scaling factor that has been applied to the font so you can
also apply it to your calculation.

I do this in order to center strings in a form when printing to a HP
printer and it works fine. But when you are dealing with unknowns
like "what browser and what font and what size?", it becomes a lot
more difficult. Perhaps there is a way to know the above information
- someone else will have to tell you about that.

HTH, Russ
 
Back
Top