realbasic-nug
[Top] [All Lists]

Re: Truncating a string

To: REALbasic Network Users Group <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: Truncating a string
From: Theo <delete at softhome dot net>
Date: Mon, 31 Dec 01 13:12:37 +0000
>> Does anyone know of a plugin or toolbox call to truncate a string to a given
>> width in pixels?
>
>Use RB code:
>
>Function truncString(str As String, width As Integer, theFont As String,
>theSize As Integer) As String
>  Dim p As Picture
>  Dim s As String
>  Dim g As Graphics
>
>  p = NewPicture(1, 1, 1)
>  g = p.Graphics
>  
>  g.TextFont = theFont
>  g.TextSize = theSize
>  
>  s = str
>  
>  While g.StringWidth(s) > width
>    s = Left(s, Len(s) - 1)
>  Wend
>  
>  Return s
>End Function

If you really want to go overboard with speed, keep a cached copy of p
and g, and use the "By-half" searching technique, a bit like the number
guessing game instead of using left-1. Imagine you are passed a 1MB
string, and width is maybe 600 pixels. Well, you'd have to wait a
while.

Some RB functions to handle this would be good. Something like:

CharPos = g.CharAtWidth(s, width)

--
    This email was probably cleaned with Email Cleaner, by:
    Theodore H. Smith - Macintosh Consultant / Contractor.
    My website: <www.elfdata.com/>



<Prev in Thread] Current Thread [Next in Thread>