string resizing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt MacDonald

    string resizing

    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


  • Suresh

    #2
    RE: string resizing

    I don't think that's possible.

    Your best bet is to truncate the string.

    "Matt MacDonald" wrote:
    [color=blue]
    > 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
    >
    >
    >[/color]

    Comment

    • Hans Kesting

      #3
      Re: string resizing


      "Matt MacDonald" <mattsmac@hotma il.com> wrote in message news:u44VrV9VEH A.1656@TK2MSFTN GP09.phx.gbl...[color=blue]
      > 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
      >
      >[/color]

      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.Measur eString 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


      Comment

      • Russ

        #4
        Re: string resizing

        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

        On Mon, 21 Jun 2004 17:19:18 -0400, "Matt MacDonald"
        <mattsmac@hotma il.com> wrote:
        [color=blue]
        >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
        >[/color]

        Comment

        Working...