TextEdit width

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

TextEdit width

pdigonzelli
Hi all ,

I have to resolve the following question: I want a TextEdit with a width
that exactly contains for example 15 characters.
Does anyone know how to do this?

TIA
Pablo


Reply | Threaded
Open this post in threaded view
|

Re: TextEdit width

Christopher J. Demers
"Pablo Digonzelli" <[hidden email]> wrote in message
news:[hidden email]...
> Hi all ,
>
> I have to resolve the following question: I want a TextEdit with a width
> that exactly contains for example 15 characters.
> Does anyone know how to do this?

Here is an example that may do what you want:
======================
shell := Shell show.
shell view extent: 200@100.
te := TextEdit new.
shell view addSubView: te.
te text: '12345'.
"Make the TextEdit just big enough to fit the text."
te extent: te calculateExtent.
======================
If you are using a proportional font then the width may vary depending upon
_which_ characters there are, not just the number of characters.  You can
look at the code in the calculateExtent method if you want to do this
without setting the text.

Chris


Reply | Threaded
Open this post in threaded view
|

Re: TextEdit width

Bill Dargel
In reply to this post by pdigonzelli
Pablo Digonzelli wrote:
> I have to resolve the following question: I want a TextEdit with a width
> that exactly contains for example 15 characters.
> Does anyone know how to do this?

You may find the following method helpful. I added it so that in unit
tests, I could check the number of characters that would be visible in a
TextEdit.

Note that it only _really_ works for a fixed width font. With variable
width fonts, how many characters you'll get depends on what the
characters are. Though if you're only using digits, most fonts make all
digits the same width.

I, for example, will use "Font name: 'Courier New' pointSize: 11" in a
text edit, and then the required width for the text edit is: "9 *
numberOfColumns + 6".

-Bill
-------------------

!TextEdit methodsFor!

numberOfTextColumns
        "Answer number of text columns which can be displayed in the width of
the TextEdit.
        Note that fractional parts of characters don't seem to be displayed, so
best to not be much over
        an integer value, or the blank at the right of the view makes it appear
that there is no character,
        where there might actually be one.
        Note - Must set the font into the canvas as part of the same operation,
as the canvas seems to be very transitory."

        | textMetrics |
        textMetrics := (self canvas)
                                font: self font;
                                textMetrics.
        ^(self clientWidth - 2) / textMetrics tmAveCharWidth asFloat

        "Could also do something with (textMetrics tmMaxCharWidth) "! !

-------------------------------------------
Bill Dargel            [hidden email]
Shoshana Technologies
100 West Joy Road, Ann Arbor, MI 48105  USA


Reply | Threaded
Open this post in threaded view
|

Re: TextEdit width

Bill Dargel
In reply to this post by Christopher J. Demers
Christopher J. Demers wrote:
> Here is an example that may do what you want:
> [snip]
> te extent: te calculateExtent.

This is an amazing newsgroup. Even when you think you know (at least
something) about a subject, someone else will come along with another
(better) answer that shows you something that you missed. Thanks Chris!

-------------------------------------------
Bill Dargel            [hidden email]
Shoshana Technologies
100 West Joy Road, Ann Arbor, MI 48105  USA


Reply | Threaded
Open this post in threaded view
|

Re: TextEdit width

pdigonzelli
In reply to this post by Christopher J. Demers
Thanks Chris

That works very well

Pablo

"Christopher J. Demers" <[hidden email]> escribió en el
mensaje news:[hidden email]...

> "Pablo Digonzelli" <[hidden email]> wrote in message
> news:[hidden email]...
> > Hi all ,
> >
> > I have to resolve the following question: I want a TextEdit with a width
> > that exactly contains for example 15 characters.
> > Does anyone know how to do this?
>
> Here is an example that may do what you want:
> ======================
> shell := Shell show.
> shell view extent: 200@100.
> te := TextEdit new.
> shell view addSubView: te.
> te text: '12345'.
> "Make the TextEdit just big enough to fit the text."
> te extent: te calculateExtent.
> ======================
> If you are using a proportional font then the width may vary depending
upon
> _which_ characters there are, not just the number of characters.  You can
> look at the code in the calculateExtent method if you want to do this
> without setting the text.
>
> Chris
>
>