Clearing an NSTextView

  • Hey,

    I have a small iTunes application and I am adding supporting the
    display of lyrics. Now I have it set up just like I want it to, and it
    looks amazing. Now the only problem is this:

    When I use [NSTextView insertText:@"Lyrics Code Here"];, it doesn't
    delete the current text so that is it is replaced for the user... So
    after opening the lyrics for quite a few songs, it is a bundled up
    into one long set of lyrics. I looked at both NSTextView and the
    classes referenced by the insertText documentation. So how can I
    remove all the current text and replace it using insertText?

    Thanks,
    Jeremy
    "For a long time it puzzled me how something so expensive, so leading
    edge, could be so useless, and then it occurred to me that a computer
    is a stupid machine with the ability to do incredibly smart things,
    while computer programmers are smart people with the ability to do
    incredibly stupid things. They are, in short, a perfect match." - Bill
    Bryson
  • [textView setString:@"Lyrics Code Here"];

    Jeremy wrote:
    > Hey,
    >
    > I have a small iTunes application and I am adding supporting the display
    > of lyrics. Now I have it set up just like I want it to, and it looks
    > amazing. Now the only problem is this:
    >
    > When I use [NSTextView insertText:@"Lyrics Code Here"];, it doesn't
    > delete the current text so that is it is replaced for the user... So
    > after opening the lyrics for quite a few songs, it is a bundled up into
    > one long set of lyrics. I looked at both NSTextView and the classes
    > referenced by the insertText documentation. So how can I remove all the
    > current text and replace it using insertText?
    >
    > Thanks,
    > Jeremy
    > "For a long time it puzzled me how something so expensive, so leading
    > edge, could be so useless, and then it occurred to me that a computer is
    > a stupid machine with the ability to do incredibly smart things, while
    > computer programmers are smart people with the ability to do incredibly
    > stupid things. They are, in short, a perfect match." - Bill Bryson
    >
  • > When I use [NSTextView insertText:@"Lyrics Code Here"];, it doesn't
    > delete the current text so that is it is replaced for the user...

      Why would you believe "insert" means anything but? If you read the
    documentation on the text system, you'll get a better idea of where to
    look for more answers.

      You can ask the text view for its NSTextStorage (which is a
    subclass of NSMutableAttributedString) and replace the contents with -
    replaceCharactersInRange:withString: or -
    replaceCharactersInRange:withAttributedString:

    --
    I.S.
  • I.S.,

    I knew insertText: wouldn't remove the text. I just can't figure out
    how to remove it all. And how do I select all of characters? That
    would be my only problem with the
    replaceCharactersInRange:withString:...

    Thanks,
    Jeremy
    "For a long time it puzzled me how something so expensive, so leading
    edge, could be so useless, and then it occurred to me that a computer
    is a stupid machine with the ability to do incredibly smart things,
    while computer programmers are smart people with the ability to do
    incredibly stupid things. They are, in short, a perfect match." - Bill
    Bryson

    On Feb 24, 2008, at 3:20 PM, I. Savant wrote:

    >> When I use [NSTextView insertText:@"Lyrics Code Here"];, it doesn't
    >> delete the current text so that is it is replaced for the user...
    >
    >
    > Why would you believe "insert" means anything but? If you read the
    > documentation on the text system, you'll get a better idea of where
    > to look for more answers.
    >
    > You can ask the text view for its NSTextStorage (which is a
    > subclass of NSMutableAttributedString) and replace the contents with
    > -replaceCharactersInRange:withString: or -
    > replaceCharactersInRange:withAttributedString:
    >
    > --
    > I.S.
    >
    >
  • On Feb 24, 2008, at 3:16 PM, Gary L. Wade wrote:

    > [textView setString:@"Lyrics Code Here"];

      Of course this works too. :-) The caveat is that if you have
    formatting applied, this may interfere. There are similar NSText
    methods such as -replaceCharactersInRange:withString: and the like.
    These also give no control over formatting.

      If you're worried about preserving formatting (when you have
    "allows multiple fonts" enabled, etc.) you'll find it easier to modify
    the underlying NSTextStorage.

    --
    I.S.
  • > I knew insertText: wouldn't remove the text. I just can't figure out
    > how to remove it all.

      Did you read my previous e-mail? See also the e-mail from Gary. The
    approach you take depends on whether you're using any attributes
    (formatting). If it's all the same font/size/etc. then setString: will
    do just fine ... just set it to:

        @"";

      ... and it's empty.

    > And how do I select all of characters? That would be my only problem
    > with the replaceCharactersInRange:withString:...

      -setSelectedRange:?

    --
    I.S.

    On Feb 24, 2008, at 3:23 PM, Jeremy wrote:

    > I.S.,
    >
    > I knew insertText: wouldn't remove the text. I just can't figure out
    > how to remove it all. And how do I select all of characters? That
    > would be my only problem with the
    > replaceCharactersInRange:withString:...
    >
    > Thanks,
    > Jeremy
    > "For a long time it puzzled me how something so expensive, so
    > leading edge, could be so useless, and then it occurred to me that a
    > computer is a stupid machine with the ability to do incredibly smart
    > things, while computer programmers are smart people with the ability
    > to do incredibly stupid things. They are, in short, a perfect
    > match." - Bill Bryson
    >
    >
    > On Feb 24, 2008, at 3:20 PM, I. Savant wrote:
    >
    >>> When I use [NSTextView insertText:@"Lyrics Code Here"];, it
    >>> doesn't delete the current text so that is it is replaced for the
    >>> user...
    >>
    >>
    >> Why would you believe "insert" means anything but? If you read the
    >> documentation on the text system, you'll get a better idea of where
    >> to look for more answers.
    >>
    >> You can ask the text view for its NSTextStorage (which is a
    >> subclass of NSMutableAttributedString) and replace the contents
    >> with -replaceCharactersInRange:withString: or -
    >> replaceCharactersInRange:withAttributedString:
    >>
    >> --
    >> I.S.
    >>
    >>

  • >> And how do I select all of characters? That would be my only
    >> problem with the replaceCharactersInRange:withString:...
    >
    > -setSelectedRange:?

      Sorry, I misunderstood your question. Why would you need to
    'select' the characters to use -replaceCharactersInRange:withString:?
    You give it the entire range of the text currently in the text view
    and a new string to replace it with.

      No selection necessary if none is desired ...

    --
    I.S.
  • IS,

    Thanks for the setString:. I believe I tried that before and it didn't
    work. It is now working as intended.

    Jeremy
    "For a long time it puzzled me how something so expensive, so leading
    edge, could be so useless, and then it occurred to me that a computer
    is a stupid machine with the ability to do incredibly smart things,
    while computer programmers are smart people with the ability to do
    incredibly stupid things. They are, in short, a perfect match." - Bill
    Bryson

    On Feb 24, 2008, at 3:30 PM, I. Savant wrote:

    >> I knew insertText: wouldn't remove the text. I just can't figure
    >> out how to remove it all.
    >
    > Did you read my previous e-mail? See also the e-mail from Gary. The
    > approach you take depends on whether you're using any attributes
    > (formatting). If it's all the same font/size/etc. then setString:
    > will do just fine ... just set it to:
    >
    > @"";
    >
    > ... and it's empty.
    >
    >> And how do I select all of characters? That would be my only
    >> problem with the replaceCharactersInRange:withString:...
    >
    > -setSelectedRange:?
    >
    > --
    > I.S.
    >
    >
    >
    > On Feb 24, 2008, at 3:23 PM, Jeremy wrote:
    >
    >> I.S.,
    >>
    >> I knew insertText: wouldn't remove the text. I just can't figure
    >> out how to remove it all. And how do I select all of characters?
    >> That would be my only problem with the
    >> replaceCharactersInRange:withString:...
    >>
    >> Thanks,
    >> Jeremy
    >> "For a long time it puzzled me how something so expensive, so
    >> leading edge, could be so useless, and then it occurred to me that
    >> a computer is a stupid machine with the ability to do incredibly
    >> smart things, while computer programmers are smart people with the
    >> ability to do incredibly stupid things. They are, in short, a
    >> perfect match." - Bill Bryson
    >>
    >>
    >> On Feb 24, 2008, at 3:20 PM, I. Savant wrote:
    >>
    >>>> When I use [NSTextView insertText:@"Lyrics Code Here"];, it
    >>>> doesn't delete the current text so that is it is replaced for the
    >>>> user...
    >>>
    >>>
    >>> Why would you believe "insert" means anything but? If you read the
    >>> documentation on the text system, you'll get a better idea of
    >>> where to look for more answers.
    >>>
    >>> You can ask the text view for its NSTextStorage (which is a
    >>> subclass of NSMutableAttributedString) and replace the contents
    >>> with -replaceCharactersInRange:withString: or -
    >>> replaceCharactersInRange:withAttributedString:
    >>>
    >>> --
    >>> I.S.
    >>>
    >>>

    >
  • > Thanks for the setString:. I believe I tried that before and it
    > didn't work. It is now working as intended.

      Thank Gary for that - I didn't even think about it until he
    mentioned it because for some reason I was thinking of "maximum
    control" (assuming formatting). I'm a bit of a control freak ... ;-)

    --
    I.S.
  • Personally, I didn't get an e-mail from Gary on this subject. I'm sure
    one was sent but I only have e-mails from you IS...

    Jeremy
    "For a long time it puzzled me how something so expensive, so leading
    edge, could be so useless, and then it occurred to me that a computer
    is a stupid machine with the ability to do incredibly smart things,
    while computer programmers are smart people with the ability to do
    incredibly stupid things. They are, in short, a perfect match." - Bill
    Bryson

    On Feb 24, 2008, at 3:38 PM, I. Savant wrote:

    >> Thanks for the setString:. I believe I tried that before and it
    >> didn't work. It is now working as intended.
    >
    > Thank Gary for that - I didn't even think about it until he
    > mentioned it because for some reason I was thinking of "maximum
    > control" (assuming formatting). I'm a bit of a control freak ... ;-)
    >
    > --
    > I.S.
    >
    >