How to apply a new font to NSTextField after a font change

  • Hi,

    How can I apply a new font to NSTextField after a font change?

    There is a NSTextField which doesn't have rich text style.  Its font
    is big and it contains some texts.

    Then I change the font to another one like:

      NSFont* font = [NSFont fontWithName:@"Arial" size:10.0];
      [text setFont:font];
      NSSize size = [text frame].size;
      size.height = ceil([font ascender]) - floor([font descender]) + 5;
      [text setFrameSize:size];

    Though I set a new font, the text in the NSTextField remains in the
    old font.  If I push the enter key, the style is applied to the
    NSTextField.

    I want to apply a new font without hitting the enter key.  What should
    I do?

    Reproduce code:
      http://limechat.net/sample/TextFieldHeight.zip

    --
    Satoshi Nakagawa
  • Not sure if this will fix it, but did you try [text setNeedsDisplay:YES]
    ?

    Satoshi Nakagawa wrote:
    > Hi,
    >
    >
    > How can I apply a new font to NSTextField after a font change?
    >
    > There is a NSTextField which doesn't have rich text style.  Its font
    > is big and it contains some texts.
    >
    > Then I change the font to another one like:
    >
    > NSFont* font = [NSFont fontWithName:@"Arial" size:10.0];
    > [text setFont:font];
    > NSSize size = [text frame].size;
    > size.height = ceil([font ascender]) - floor([font descender]) + 5;
    > [text setFrameSize:size];
    >
    > Though I set a new font, the text in the NSTextField remains in the
    > old font.  If I push the enter key, the style is applied to the
    > NSTextField.
    >
    > I want to apply a new font without hitting the enter key.  What should
    > I do?
    >
    > Reproduce code:
    > http://limechat.net/sample/TextFieldHeight.zip
    >
    > --
    > Satoshi Nakagawa
  • Sure, I tried it but it didn't work.

    --
    Satoshi Nakagawa

    On 2007/12/08, at 2:53, John Stiles wrote:

    > Not sure if this will fix it, but did you try [text
    > setNeedsDisplay:YES]
    > ?
    >
    >
    > Satoshi Nakagawa wrote:
    >> Hi,
    >>
    >>
    >> How can I apply a new font to NSTextField after a font change?
    >>
    >> There is a NSTextField which doesn't have rich text style.  Its
    >> font is big and it contains some texts.
    >>
    >> Then I change the font to another one like:
    >>
    >> NSFont* font = [NSFont fontWithName:@"Arial" size:10.0];
    >> [text setFont:font];
    >> NSSize size = [text frame].size;
    >> size.height = ceil([font ascender]) - floor([font descender]) + 5;
    >> [text setFrameSize:size];
    >>
    >> Though I set a new font, the text in the NSTextField remains in the
    >> old font.  If I push the enter key, the style is applied to the
    >> NSTextField.
    >>
    >> I want to apply a new font without hitting the enter key.  What
    >> should I do?
    >>
    >> Reproduce code:
    >> http://limechat.net/sample/TextFieldHeight.zip
    >>
    >> --
    >> Satoshi Nakagawa
  • Is this a text field that is currently being edited?
    If so, you will need to work with the window's field editor, since that
    is standing in for the NSTextField while it is being edited.

    Satoshi Nakagawa wrote:
    > Sure, I tried it but it didn't work.
    >
    > --
    > Satoshi Nakagawa
    >
    > On 2007/12/08, at 2:53, John Stiles wrote:
    >
    >> Not sure if this will fix it, but did you try [text setNeedsDisplay:YES]
    >> ?
    >>
    >>
    >> Satoshi Nakagawa wrote:
    >>> Hi,
    >>>
    >>>
    >>> How can I apply a new font to NSTextField after a font change?
    >>>
    >>> There is a NSTextField which doesn't have rich text style.  Its font
    >>> is big and it contains some texts.
    >>>
    >>> Then I change the font to another one like:
    >>>
    >>> NSFont* font = [NSFont fontWithName:@"Arial" size:10.0];
    >>> [text setFont:font];
    >>> NSSize size = [text frame].size;
    >>> size.height = ceil([font ascender]) - floor([font descender]) + 5;
    >>> [text setFrameSize:size];
    >>>
    >>> Though I set a new font, the text in the NSTextField remains in the
    >>> old font.  If I push the enter key, the style is applied to the
    >>> NSTextField.
    >>>
    >>> I want to apply a new font without hitting the enter key.  What
    >>> should I do?
    >>>
    >>> Reproduce code:
    >>> http://limechat.net/sample/TextFieldHeight.zip
    >>>
    >>> --
    >>> Satoshi Nakagawa
    >
  • Yes, but how?
    I could not apply a new font quickly by the code below:

      NSFont* font = [NSFont fontWithName:@"Arial" size:10.0];
      [text setFont:font];
      NSSize size = [text frame].size;
      size.height = ceil([font ascender]) - floor([font descender]) + 5;
      [text setFrameSize:size];

      NSText* e = [text currentEditor];
      [e setRichText:YES];
      NSRange range;
      range.location = 0;
      range.length = [[e string] length];
      [e setFont:font range:range];
      [e setRichText:NO];

      [text setNeedsDisplay:YES];

    --
    Satoshi Nakagawa

    On 2007/12/08, at 3:18, John Stiles wrote:

    > Is this a text field that is currently being edited?
    > If so, you will need to work with the window's field editor, since
    > that is standing in for the NSTextField while it is being edited.
    >
    >
    > Satoshi Nakagawa wrote:
    >> Sure, I tried it but it didn't work.
    >>
    >> --
    >> Satoshi Nakagawa
    >>
    >> On 2007/12/08, at 2:53, John Stiles wrote:
    >>
    >>> Not sure if this will fix it, but did you try [text
    >>> setNeedsDisplay:YES]
    >>> ?
    >>>
    >>>
    >>> Satoshi Nakagawa wrote:
    >>>> Hi,
    >>>>
    >>>>
    >>>> How can I apply a new font to NSTextField after a font change?
    >>>>
    >>>> There is a NSTextField which doesn't have rich text style.  Its
    >>>> font is big and it contains some texts.
    >>>>
    >>>> Then I change the font to another one like:
    >>>>
    >>>> NSFont* font = [NSFont fontWithName:@"Arial" size:10.0];
    >>>> [text setFont:font];
    >>>> NSSize size = [text frame].size;
    >>>> size.height = ceil([font ascender]) - floor([font descender]) + 5;
    >>>> [text setFrameSize:size];
    >>>>
    >>>> Though I set a new font, the text in the NSTextField remains in
    >>>> the old font.  If I push the enter key, the style is applied to
    >>>> the NSTextField.
    >>>>
    >>>> I want to apply a new font without hitting the enter key.  What
    >>>> should I do?
    >>>>
    >>>> Reproduce code:
    >>>> http://limechat.net/sample/TextFieldHeight.zip
    >>>>
    >>>> --
    >>>> Satoshi Nakagawa
    >>>>
  • Finally I resolve the problem by:

      [text setAttributedString:[[[NSAttributedString alloc]
    initWithString:@""] autorelease]];

    Thanks.

    --
    Satoshi Nakagawa

    On 2007/12/08, at 3:27, Satoshi Nakagawa wrote:

    > Yes, but how?
    > I could not apply a new font quickly by the code below:
    >
    > NSFont* font = [NSFont fontWithName:@"Arial" size:10.0];
    > [text setFont:font];
    > NSSize size = [text frame].size;
    > size.height = ceil([font ascender]) - floor([font descender]) + 5;
    > [text setFrameSize:size];
    >
    > NSText* e = [text currentEditor];
    > [e setRichText:YES];
    > NSRange range;
    > range.location = 0;
    > range.length = [[e string] length];
    > [e setFont:font range:range];
    > [e setRichText:NO];
    >
    > [text setNeedsDisplay:YES];
    >
    > --
    > Satoshi Nakagawa
    >
    > On 2007/12/08, at 3:18, John Stiles wrote:
    >
    >> Is this a text field that is currently being edited?
    >> If so, you will need to work with the window's field editor, since
    >> that is standing in for the NSTextField while it is being edited.
    >>
    >>
    >> Satoshi Nakagawa wrote:
    >>> Sure, I tried it but it didn't work.
    >>>
    >>> --
    >>> Satoshi Nakagawa
    >>>
    >>> On 2007/12/08, at 2:53, John Stiles wrote:
    >>>
    >>>> Not sure if this will fix it, but did you try [text
    >>>> setNeedsDisplay:YES]
    >>>> ?
    >>>>
    >>>>
    >>>> Satoshi Nakagawa wrote:
    >>>>> Hi,
    >>>>>
    >>>>>
    >>>>> How can I apply a new font to NSTextField after a font change?
    >>>>>
    >>>>> There is a NSTextField which doesn't have rich text style.  Its
    >>>>> font is big and it contains some texts.
    >>>>>
    >>>>> Then I change the font to another one like:
    >>>>>
    >>>>> NSFont* font = [NSFont fontWithName:@"Arial" size:10.0];
    >>>>> [text setFont:font];
    >>>>> NSSize size = [text frame].size;
    >>>>> size.height = ceil([font ascender]) - floor([font descender]) + 5;
    >>>>> [text setFrameSize:size];
    >>>>>
    >>>>> Though I set a new font, the text in the NSTextField remains in
    >>>>> the old font.  If I push the enter key, the style is applied to
    >>>>> the NSTextField.
    >>>>>
    >>>>> I want to apply a new font without hitting the enter key.  What
    >>>>> should I do?
    >>>>>
    >>>>> Reproduce code:
    >>>>> http://limechat.net/sample/TextFieldHeight.zip
    >>>>>
    >>>>> --
    >>>>> Satoshi Nakagawa
    >>>>>

previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today