Trouble with formattedWidth revisited

Graham Samuel livfoss at mac.com
Thu Apr 6 16:50:08 EDT 2006


Recently, I wrote:

> I'm interested in truncating a character string to a certain number  
> of pixels. As part of this process, I put the string into field and  
> check the field's formattedWidth.  My script says
>
>   put the formattedWidth of field "myField" into myTemp
>
> I can verify that the string is really there and is not null.
>
> The RR docs says:
>
>> If you specify a field, the formattedWidth reports the width  
>> required by the field's text.
>
> Well, for me sometimes it does, and sometimes it doesn't. When it  
> doesn't, the formattedWidth reports zero. I guess this is something  
> to do with me forgetting to give any specific attributes of font or  
> type size to the field (or the string), but what rules then apply?  
> I am finding this very mysterious. I am pretty sure I can cook up a  
> foolproof recipe for getting the zero, but before I do maybe  
> someone could explain what is likely to be happening here.

Sarah Reichelt (thank, Sarah) suggested my problems would go away if  
I made sure the field had its 'dontWrap' property set to true. Well,  
I've done this, and I've made sure there is a genuine font set  
(Lucida Grande) and a genuine text size (10 point), and when I run my  
handler it returns zero. I've stepped through it using the debugger,  
and I know there really is a character string in the field, but the  
formattedWidth is reported as zero. If I abort the function and use  
the message box to get the formattedWidth I get the same result,  
although if I use the message box to get the number of characters in  
the field, or to show the contents, I get the expected non-empty  
string. I've tried moving the field to a different stack, but this  
didn't do anything. Tests I've tried to do within a single mainstack  
don't exhibit the problem.

So far the only way I can get it to work is by going to the card with  
the field on it before doing the formattedWidth test (which seems to  
suggest it only works on 'this card'), but that's something I  
certainly don't want to do. The relevant stack is open, and I had no  
difficulty writing to the field.

It's got to be something I'm doing wrong, but I simply can't work out  
what it is.

Any suggestions out there?

Graham

----------------------------------------
Graham Samuel / The Living Fossil Co. / UK and France




More information about the use-livecode mailing list