Silly NSTextFieldCell tricks
-
I've noticed that in table views, the NSTextFieldCell has the ability to
ever-so-slightly squash its contents horizontally, to fit inside the
table cell, before eventually giving up and truncating them with ellipses.
Is there any way for me to programmatically do the same thing to my own
NSTextFieldCell?
As I'm typing this, suddenly I'm thinking "maybe I could fake it by
altering the cell's bounds while leaving the frame alone"… but is there
a better way? Changing the bounds would make the text thinner, which
isn't quite the same as squeezing the letters more tightly together. -
On Jan 31, 2008, at 11:27 AM, John Stiles wrote:
> I've noticed that in table views, the NSTextFieldCell has the
> ability to ever-so-slightly squash its contents horizontally, to
> fit inside the table cell, before eventually giving up and
> truncating them with ellipses.
>
> Is there any way for me to programmatically do the same thing to my
> own NSTextFieldCell?
>
> As I'm typing this, suddenly I'm thinking "maybe I could fake it by
> altering the cell's bounds while leaving the frame alone"… but is
> there a better way? Changing the bounds would make the text
> thinner, which isn't quite the same as squeezing the letters more
> tightly together.
Hi John,
This is called "tightening" in the API, and it's enabled by default
on text fields (actually, on any control) when the line break mode is
one of the three truncation line break modes in NSParagraphStyle.h.
You can control the threshold between tightening and truncation with
the setTighteningFactorForTruncation: method on NSMutableParagraphStyle.
Hope that helps,
-Peter -
On Jan 31, 2008, at 1:27 PM, John Stiles wrote:
> I've noticed that in table views, the NSTextFieldCell has the
> ability to ever-so-slightly squash its contents horizontally, to fit
> inside the table cell, before eventually giving up and truncating
> them with ellipses.
>
> Is there any way for me to programmatically do the same thing to my
> own NSTextFieldCell?
>
> As I'm typing this, suddenly I'm thinking "maybe I could fake it by
> altering the cell's bounds while leaving the frame alone"… but is
> there a better way? Changing the bounds would make the text thinner,
> which isn't quite the same as squeezing the letters more tightly
> together.
With attributed strings, you can set the paragraph style to be one
whose setLineBreakMode is one of the three truncation modes (which
provides the ellipsis), and use can even use
setTighteningFactorForTruncation to adjust how much it tries to reduce
the intercharacter spacing before switching to inserting an ellipsis.
You can probably also manually change NSKernAttributeName property of
the strings attributes as well.
Glenn Andreas <gandreas...>
<http://www.gandreas.com/> wicked fun!
quadrium | prime : build, mutate, evolve, animate : the next
generation of fractal art -
Peter Ammon wrote:
>Cool!
> On Jan 31, 2008, at 11:27 AM, John Stiles wrote:
>
>> I've noticed that in table views, the NSTextFieldCell has the ability
>> to ever-so-slightly squash its contents horizontally, to fit inside
>> the table cell, before eventually giving up and truncating them with
>> ellipses.
>>
>> Is there any way for me to programmatically do the same thing to my
>> own NSTextFieldCell?
>>
>> As I'm typing this, suddenly I'm thinking "maybe I could fake it by
>> altering the cell's bounds while leaving the frame alone"… but is
>> there a better way? Changing the bounds would make the text thinner,
>> which isn't quite the same as squeezing the letters more tightly
>> together.
>
> Hi John,
>
> This is called "tightening" in the API, and it's enabled by default on
> text fields (actually, on any control) when the line break mode is one
> of the three truncation line break modes in NSParagraphStyle.h. You
> can control the threshold between tightening and truncation with the
> setTighteningFactorForTruncation: method on NSMutableParagraphStyle.
>
> Hope that helps,
> -Peter
Is there any way to enable this for an NSTextField in a window? Someone
off-list suggested using attributed strings to do this, but is that the
best way? -
On Jan 31, 2008, at 11:56 AM, John Stiles wrote:
> Peter Ammon wrote:
>>
>> On Jan 31, 2008, at 11:27 AM, John Stiles wrote:
>>
>>> I've noticed that in table views, the NSTextFieldCell has the
>>> ability to ever-so-slightly squash its contents horizontally, to
>>> fit inside the table cell, before eventually giving up and
>>> truncating them with ellipses.
>>>
>>> Is there any way for me to programmatically do the same thing to
>>> my own NSTextFieldCell?
>>>
>>> As I'm typing this, suddenly I'm thinking "maybe I could fake it
>>> by altering the cell's bounds while leaving the frame alone"… but
>>> is there a better way? Changing the bounds would make the text
>>> thinner, which isn't quite the same as squeezing the letters more
>>> tightly together.
>>
>> Hi John,
>>
>> This is called "tightening" in the API, and it's enabled by
>> default on text fields (actually, on any control) when the line
>> break mode is one of the three truncation line break modes in
>> NSParagraphStyle.h. You can control the threshold between
>> tightening and truncation with the
>> setTighteningFactorForTruncation: method on NSMutableParagraphStyle.
>>
>> Hope that helps,
>> -Peter
> Cool!
> Is there any way to enable this for an NSTextField in a window?
> Someone off-list suggested using attributed strings to do this, but
> is that the best way?
Attributed strings aren't necessary, and NSTextFields do this by
default. In IB, make an NSTextField and in the Inspector, set its
Line Breaks mode to Truncate Tail, and make it resize with the window.
Oh, it doesn't happen if the text field has key focus, so you could
add another text field to hold the key focus for testing. If you do
that, you should see this behavior.
-Peter -
Peter Ammon wrote:
>
> On Jan 31, 2008, at 11:56 AM, John Stiles wrote:
>
>> Peter Ammon wrote:
>>>
>>> On Jan 31, 2008, at 11:27 AM, John Stiles wrote:
>>>
>>>> I've noticed that in table views, the NSTextFieldCell has the
>>>> ability to ever-so-slightly squash its contents horizontally, to
>>>> fit inside the table cell, before eventually giving up and
>>>> truncating them with ellipses.
>>>>
>>>> Is there any way for me to programmatically do the same thing to my
>>>> own NSTextFieldCell?
>>>>
>>>> As I'm typing this, suddenly I'm thinking "maybe I could fake it by
>>>> altering the cell's bounds while leaving the frame alone"… but is
>>>> there a better way? Changing the bounds would make the text
>>>> thinner, which isn't quite the same as squeezing the letters more
>>>> tightly together.
>>>
>>> Hi John,
>>>
>>> This is called "tightening" in the API, and it's enabled by default
>>> on text fields (actually, on any control) when the line break mode
>>> is one of the three truncation line break modes in
>>> NSParagraphStyle.h. You can control the threshold between
>>> tightening and truncation with the setTighteningFactorForTruncation:
>>> method on NSMutableParagraphStyle.
>>>
>>> Hope that helps,
>>> -Peter
>> Cool!
>> Is there any way to enable this for an NSTextField in a window?
>> Someone off-list suggested using attributed strings to do this, but
>> is that the best way?
>
> Attributed strings aren't necessary, and NSTextFields do this by
> default. In IB, make an NSTextField and in the Inspector, set its
> Line Breaks mode to Truncate Tail, and make it resize with the window.
>
> Oh, it doesn't happen if the text field has key focus, so you could
> add another text field to hold the key focus for testing. If you do
> that, you should see this behavior.
Awesome. I'll give this a shot right now.
FWIW, I also experimented with setting the bounds of the cell, and this
actually worked great—better than I thought, actually. But it sounds
like I could get the behavior "for free" by just setting the text
field's tightening factor and line-break mode, so I'll try that next. -
Peter Ammon wrote:
>
> On Jan 31, 2008, at 11:56 AM, John Stiles wrote:
>
>> Peter Ammon wrote:
>>>
>>> On Jan 31, 2008, at 11:27 AM, John Stiles wrote:
>>>
>>>> I've noticed that in table views, the NSTextFieldCell has the
>>>> ability to ever-so-slightly squash its contents horizontally, to
>>>> fit inside the table cell, before eventually giving up and
>>>> truncating them with ellipses.
>>>>
>>>> Is there any way for me to programmatically do the same thing to my
>>>> own NSTextFieldCell?
>>>>
>>>> As I'm typing this, suddenly I'm thinking "maybe I could fake it by
>>>> altering the cell's bounds while leaving the frame alone"… but is
>>>> there a better way? Changing the bounds would make the text
>>>> thinner, which isn't quite the same as squeezing the letters more
>>>> tightly together.
>>>
>>> Hi John,
>>>
>>> This is called "tightening" in the API, and it's enabled by default
>>> on text fields (actually, on any control) when the line break mode
>>> is one of the three truncation line break modes in
>>> NSParagraphStyle.h. You can control the threshold between
>>> tightening and truncation with the setTighteningFactorForTruncation:
>>> method on NSMutableParagraphStyle.
>>>
>>> Hope that helps,
>>> -Peter
>> Cool!
>> Is there any way to enable this for an NSTextField in a window?
>> Someone off-list suggested using attributed strings to do this, but
>> is that the best way?
>
> Attributed strings aren't necessary, and NSTextFields do this by
> default. In IB, make an NSTextField and in the Inspector, set its
> Line Breaks mode to Truncate Tail, and make it resize with the window.
Wait, I'm confused—how do I control the tightening factor on an
NSTextField? I can set its line-break mode, but there doesn't seem to be
a way to set its paragraph style or its tightening factor. Or is this a
case where I need to use attributed strings after all? -
The tighteningFactor has appropriate value as the default.
You can just specify any of the truncating line break mode to the cell
and the rest should goes automatically.
If you need to specify your own custom factor, then, you need to use
attributed string.
Aki
On 2008/01/31, at 13:37, John Stiles wrote:
> Peter Ammon wrote:
>>
>> On Jan 31, 2008, at 11:56 AM, John Stiles wrote:
>>
>>> Peter Ammon wrote:
>>>>
>>>> On Jan 31, 2008, at 11:27 AM, John Stiles wrote:
>>>>
>>>>> I've noticed that in table views, the NSTextFieldCell has the
>>>>> ability to ever-so-slightly squash its contents horizontally, to
>>>>> fit inside the table cell, before eventually giving up and
>>>>> truncating them with ellipses.
>>>>>
>>>>> Is there any way for me to programmatically do the same thing to
>>>>> my own NSTextFieldCell?
>>>>>
>>>>> As I'm typing this, suddenly I'm thinking "maybe I could fake it
>>>>> by altering the cell's bounds while leaving the frame
>>>>> alone"$B!D(B but is there a better way? Changing the bounds
>>>>> would make the text thinner, which isn't quite the same as
>>>>> squeezing the letters more tightly together.
>>>>
>>>> Hi John,
>>>>
>>>> This is called "tightening" in the API, and it's enabled by
>>>> default on text fields (actually, on any control) when the line
>>>> break mode is one of the three truncation line break modes in
>>>> NSParagraphStyle.h. You can control the threshold between
>>>> tightening and truncation with the
>>>> setTighteningFactorForTruncation: method on
>>>> NSMutableParagraphStyle.
>>>>
>>>> Hope that helps,
>>>> -Peter
>>> Cool!
>>> Is there any way to enable this for an NSTextField in a window?
>>> Someone off-list suggested using attributed strings to do this,
>>> but is that the best way?
>>
>> Attributed strings aren't necessary, and NSTextFields do this by
>> default. In IB, make an NSTextField and in the Inspector, set its
>> Line Breaks mode to Truncate Tail, and make it resize with the
>> window.
>
> Wait, I'm confused$B!=(Bhow do I control the tightening factor on
> an NSTextField? I can set its line-break mode, but there doesn't
> seem to be a way to set its paragraph style or its tightening
> factor. Or is this a case where I need to use attributed strings
> after all?
-
Yes, I want more than 5% (the default amount).
I tried using attributed strings, and it works, but it causes other
minor problems—for instance, -setAlignment: doesn't work on the text
field any more (you'd have to put that in the paragraph style). Doh.
This does look better, visually, than changing the bounds. It means I'll
need to redo some code, but hopefully it will be worth it.
Aki Inoue wrote:
> The tighteningFactor has appropriate value as the default.
> You can just specify any of the truncating line break mode to the cell
> and the rest should goes automatically.
>
> If you need to specify your own custom factor, then, you need to use
> attributed string.
>
> Aki
>
> On 2008/01/31, at 13:37, John Stiles wrote:
>
>> Peter Ammon wrote:
>>>
>>> On Jan 31, 2008, at 11:56 AM, John Stiles wrote:
>>>
>>>> Peter Ammon wrote:
>>>>>
>>>>> On Jan 31, 2008, at 11:27 AM, John Stiles wrote:
>>>>>
>>>>>> I've noticed that in table views, the NSTextFieldCell has the
>>>>>> ability to ever-so-slightly squash its contents horizontally, to
>>>>>> fit inside the table cell, before eventually giving up and
>>>>>> truncating them with ellipses.
>>>>>>
>>>>>> Is there any way for me to programmatically do the same thing to
>>>>>> my own NSTextFieldCell?
>>>>>>
>>>>>> As I'm typing this, suddenly I'm thinking "maybe I could fake it
>>>>>> by altering the cell's bounds while leaving the frame alone"… but
>>>>>> is there a better way? Changing the bounds would make the text
>>>>>> thinner, which isn't quite the same as squeezing the letters more
>>>>>> tightly together.
>>>>>
>>>>> Hi John,
>>>>>
>>>>> This is called "tightening" in the API, and it's enabled by
>>>>> default on text fields (actually, on any control) when the line
>>>>> break mode is one of the three truncation line break modes in
>>>>> NSParagraphStyle.h. You can control the threshold between
>>>>> tightening and truncation with the
>>>>> setTighteningFactorForTruncation: method on NSMutableParagraphStyle.
>>>>>
>>>>> Hope that helps,
>>>>> -Peter
>>>> Cool!
>>>> Is there any way to enable this for an NSTextField in a window?
>>>> Someone off-list suggested using attributed strings to do this, but
>>>> is that the best way?
>>>
>>> Attributed strings aren't necessary, and NSTextFields do this by
>>> default. In IB, make an NSTextField and in the Inspector, set its
>>> Line Breaks mode to Truncate Tail, and make it resize with the window.
>>
>> Wait, I'm confused—how do I control the tightening factor on an
>> NSTextField? I can set its line-break mode, but there doesn't seem to
>> be a way to set its paragraph style or its tightening factor. Or is
>> this a case where I need to use attributed strings after all?
>



