NSMutableAttributedString binding

  • I have binded the a NSTextView to an NSAttributedString. This works
    fine when I set the binded variable, the NSTextView is showing the
    text fine.
    But if I make a change in the variable, this is not reflected in the
    NSTextView.
    I am doing this:

    [[self attributedText] replaceCharactersInRange:elementRange
    withString:[te c]];

    and the NSTextView which is binded to attributedText does never show
    the change.
    If I do an NSLog of this variable immediately following the above
    line, I can see the change.

    What should I do to get the NSTextView to show the change?

    Ivan
  • On Jan 10, 2008, at 9:11 AM, Ivan C Myrvold wrote:

    > I have binded the a NSTextView to an NSAttributedString. This works
    > fine when I set the binded variable, the NSTextView is showing the
    > text fine.
    > But if I make a change in the variable, this is not reflected in the
    > NSTextView.
    > I am doing this:
    >
    > [[self attributedText] replaceCharactersInRange:elementRange
    > withString:[te c]];
    >
    >
    > and the NSTextView which is binded to attributedText does never show
    > the change.
    > If I do an NSLog of this variable immediately following the above
    > line, I can see the change.
    >
    > What should I do to get the NSTextView to show the change?

    This isn't a case for bindings, it's a case for using the text
    system.  Each NSTextView already has a mutable attributed string, its
    text storage, and it listens for changes to the text storage
    automatically.  What you are doing is maintaining two separate copies
    of the text, and trying to have the text entirely copied over after
    each change, which would be inefficient.  Look at the text system
    documentation and see how to make changes to the text storage.

    Douglas Davidson
  • > I have binded the a NSTextView to an NSAttributedString. This works
    > fine when I set the binded variable, the NSTextView is showing the
    > text fine.
    > But if I make a change in the variable, this is not reflected in the
    > NSTextView.
    > I am doing this:
    >
    > [[self attributedText] replaceCharactersInRange:elementRange
    > withString:[te c]];
    >
    >
    > and the NSTextView which is binded to attributedText does never show
    > the change.
    > If I do an NSLog of this variable immediately following the above
    > line, I can see the change.
    >
    > What should I do to get the NSTextView to show the change?

    Even though your text view is bound to the NSMutableAttributedString,
    I'm not sure that the method you give is KVO-compliant.

    You may need [self setAttributedText:[[self attributedText]
    replaceCharactersInRange:elementRangeWithString:[te c]]];

    This is what the binding mechanism is looking for so it knows to
    update your field, you have to update our model programmatically with
    a setter or another KVO-compliant method.

    As always, tell me if I'm wrong!

    Jonathan
  • Den 10. jan.. 2008 kl. 18:28 skrev Douglas Davidson:

    >
    > On Jan 10, 2008, at 9:11 AM, Ivan C Myrvold wrote:
    >
    >> I have binded the a NSTextView to an NSAttributedString. This works
    >> fine when I set the binded variable, the NSTextView is showing the
    >> text fine.
    >> But if I make a change in the variable, this is not reflected in
    >> the NSTextView.
    >> I am doing this:
    >>
    >> [[self attributedText] replaceCharactersInRange:elementRange
    >> withString:[te c]];
    >>
    >>
    >> and the NSTextView which is binded to attributedText does never
    >> show the change.
    >> If I do an NSLog of this variable immediately following the above
    >> line, I can see the change.
    >>
    >> What should I do to get the NSTextView to show the change?
    >
    > This isn't a case for bindings, it's a case for using the text
    > system.  Each NSTextView already has a mutable attributed string,
    > its text storage, and it listens for changes to the text storage
    > automatically.  What you are doing is maintaining two separate
    > copies of the text, and trying to have the text entirely copied over
    > after each change, which would be inefficient.  Look at the text
    > system documentation and see how to make changes to the text storage.
    >
    > Douglas Davidson
    >
    >

    OK, I did follow your advice, and tried

    NSMutableAttributedString *astr = [oattributedTextView textStorage];
    [astr replaceCharactersInRange:elementRange withString:[te c]];

    where oattributedTextView is the outlet of the NSTextView.

    Now, I got the change immediately shown in my NSTextView.
    Thanks!

    Ivan
  • On Jan 10, 2008, at 11:28 AM, Ivan C Myrvold wrote:

    > OK, I did follow your advice, and tried
    >
    > NSMutableAttributedString *astr = [oattributedTextView textStorage];
    > [astr replaceCharactersInRange:elementRange withString:[te c]];
    >
    > where oattributedTextView is the outlet of the NSTextView.
    >
    > Now, I got the change immediately shown in my NSTextView.

    Be sure to read the text system documentation on working with the text
    storage.  There are a number of useful things to note--for example,
    multiple changes can be grouped for efficiency by surrounding them
    with beginEditing/endEditing calls.  If your text view is editable and
    you wish your changes to participate in undo etc. alongside other user
    changes, you need to notify the text view with should-change/did-
    change calls.

    Douglas Davidson
previous month january 2008 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