Right Alignment within Text Fields

Is it possible to make a text field align it’s contents so that when the content is too big to be displayed in the field, the left hand side is truncated?

Currently, if I change the alignment to the right, it only works if the content is smaller than the text field. If the content is larger, the contents are simply truncated at the right side.

If this can be made to work in a text field, I would also like to be able to do the same trick in a table cell.

My reason for wanting this effect is that I want to display a full path to a file in a text field, but obviously if the path is very long, the most significant part (or at least the part that is likely to be less common) of the path is the right hand end of it.

I’m sure it’s possible… jobu? :smiley:

Well, yes… everything is possible. :wink:

There are a lot of factors involved here. First, when using a text field there are only a few methods, even at the cocoa level, that govern how the text field displays text. Alignment is the only one which seems to apply to your situation. But, as you’ve said, there are limitations to the abilities of the text field, notably the ability (or lack thereof) to make the text break on the left rather than the right. Without going back on my original statement that ‘everything is possible’, this is going to be a VERY large obstacle for you under these circumstances. The text field class is actually a subclass of nscontrol, rather than nstext. It offers no actual text handling methods, so you would have to programmatically manipulate everything to get the display characteristics you want. You would have to use custom cocoa methods extensively to grab the text, measure it by it’s font/glyph-level characteristics, manually crop the string to an exact width/length, and reinsert the text into the field… all the while manually toggling the left/right alignment in case the contents do not exceed the width of the field. There are many more obstacles I found testing this out, and have yet to find a way to do this. I’m certainly no guru with obj-c, but I wouldn’t have a clue how to go about implementing this without looking up and learning all about fonts and text foundation… yawn! Resizing the text field, losing focus, inserting new text, all are obstacles that cause their own new problems.

The text view class offers some more options. There is the base writing direction, control over the insertion point, and better string evaluation abilities. Still, there isn’t any great support for this, so you’d end up writing a lot of custom code to evaluate the string, draw custom text container, blah, blah…

As far as table cells go, if you were to find a method that worked well for a text field, you could probably pretty easily transfer it over to a table text cell. Again, though, that’s a heavy programatic task.

I guess my first advice would be to find another way. Perhaps add another text field or table column to your project that could display the file name, and then use the existing one to show the full path… or vice versa. You could use a contextual menu, or popup button… you could use a panel or drawer… or find some other creative method of displaying the information you wish to convey in an easier-to-access manner. You could also manually read the string and it’s length, edit it to a length known to fit inside the field, and then chop it and re-insert it. A resizable text field would add more complications to this method.

Sorry I don’t have a good solution for you. The text foundation is an english, left-right based text structure, so finding another way is going to require some serious knowledge of cocoa to get a solid solution to this problem. I’m pretty sure that applescript doesn’t have the exact answers you’re looking for.

Good luck,
j

Thanks for the reply!

As you say, an alternative way of displaying it sounds like the way to go.

I don’t suppose you have any idea of how I could determine the size of a string of text so that I could resize a table column to make sure it was big enough to contain it’s items. I imagine it isn’t a trivial task either, but I guess it would be easier than the alignment problem!

Thanks again for taking the trouble to explain.