Text wrap in NSTextView-> how to get wrapped NSString

  • Hello,
    I would to simulate wrapped nstextview. I found I can resize used text
    area and set my preferred wrapstyle but when I get out the relative
    NSString it still remain unwrapped (correct it's right). Now I would
    to get, for each line in NSTextView what character is the last of the
    line, this screenshot should be clear
    <http://img526.imageshack.us/img526/440/wrappingrq2.png>
    so I'll add and \n to effective have the wrapped nsstring.
    Anyone can help me?
    Thanks a lot
    mlc
  • On Oct 22, 2007, at 10:13 AM, malcom wrote:

    > Hello,
    > I would to simulate wrapped nstextview. I found I can resize used text
    > area and set my preferred wrapstyle but when I get out the relative
    > NSString it still remain unwrapped (correct it's right). Now I would
    > to get, for each line in NSTextView what character is the last of the
    > line, this screenshot should be clear
    > <http://img526.imageshack.us/img526/440/wrappingrq2.png>
    > so I'll add and \n to effective have the wrapped nsstring.
    > Anyone can help me?

    There are a whole bunch of methods that NSLayoutManager provides for
    this sort of thing, but it is important to realize that your approach
    is going to have problems:

    1) If the text is fully justified, you will loose that (since the
    last line of a paragraph is not fully justified, and by adding a
    return character you'll make all of the lines the last line)
    2) If there is any extra paragraph space (see NSParagraph's
    paragraphSpacing  and paragraphSpacingBefore), this will cause all
    the text to have different line spacing (since again, everything
    becomes a paragraph and the space between lines of a paragraph would
    be different than the spaces between paragraphs)
    3) Any sort of indenting for the first line (NSParagraph's
    firstLineHeadIndent) will cause all of your lines to become indented.

    More importantly, why are you trying to "simulate wrapped
    NSTextView"?  Why can't you use a real wrapped NSTextView?

    Glenn Andreas                      <gandreas...>
      <http://www.gandreas.com/> wicked fun!
    quadrium2 | build, mutate, evolve, animate  | images, textures,
    fractals, art
  • > More importantly, why are you trying to "simulate wrapped
    > NSTextView"?  Why can't you use a real wrapped NSTextView?

    The problem is I need to take the wrapped string out to NSString. If I
    try to use NSTextView string] the returned string is unwrapped
    (because the wrapping is only visible at UI level but the string
    itself remain unwrapped). Instead I would to have the wrapped string.
    Is this possible?
  • On Oct 22, 2007, at 10:49 AM, malcom wrote:

    >> More importantly, why are you trying to "simulate wrapped
    >> NSTextView"?  Why can't you use a real wrapped NSTextView?
    >
    > The problem is I need to take the wrapped string out to NSString. If I
    > try to use NSTextView string] the returned string is unwrapped
    > (because the wrapping is only visible at UI level but the string
    > itself remain unwrapped). Instead I would to have the wrapped string.
    > Is this possible?

    Check out the "TipWrapper" sample code: <http://developer.apple.com/
    samplecode/TipWrapper/listing5.html
    >

    Glenn Andreas                      <gandreas...>
      <http://www.gandreas.com/> wicked fun!
    quadrium2 | build, mutate, evolve, animate  | images, textures,
    fractals, art
  • > Check out the "TipWrapper" sample code: <http://developer.apple.com/
    > samplecode/TipWrapper/listing5.html>

    Just looked but it uses two different nstextview and a subclass of
    nsmutablestring used to wrap text from nstextview a to final
    destination nstextview b (wrapped). It does not work with a single
    nstextview.
  • > Just looked but it uses two different nstextview and a subclass of
    > nsmutablestring used to wrap text from nstextview a to final
    > destination nstextview b (wrapped). It does not work with a single
    > nstextview.

      So, applying the following to your existing text view doesn't help?

        NSTextView    *tv;
        NSArray      *lines;

        //  Get the individual lines as laid out by the text-view with
    (soft or hard) newlines.
        lines = [tv lines];

        //  Return a single string with hard newlines in all the places
    where the text-view broke it
        return [lines componentsJoinedByString: @"\n"];

    --
    I.S.
  • On Oct 22, 2007, at 11:13 AM, I. Savant wrote:

    >> Just looked but it uses two different nstextview and a subclass of
    >> nsmutablestring used to wrap text from nstextview a to final
    >> destination nstextview b (wrapped). It does not work with a single
    >> nstextview.
    >
    > So, applying the following to your existing text view doesn't help?
    >
    > NSTextView    *tv;
    > NSArray      *lines;
    >
    > //  Get the individual lines as laid out by the text-view with
    > (soft or hard) newlines.
    > lines = [tv lines];
    >
    > //  Return a single string with hard newlines in all the places
    > where the text-view broke it
    > return [lines componentsJoinedByString: @"\n"];

    More accurately, you'd need to first add the NSTextView(Lines)
    category to provide the [-lines] functionality (which is found in
    <http://developer.apple.com/samplecode/TipWrapper/listing7.html>, and
    does all the dirty work)

    Also, the example does not use a subclass of NSMutableString, nor
    does it use two different NSTextView's - NSString(Wrapping) uses a
    single (reusable) NSTextView, and returns the wrapped text.

    So the OP should take another look at those files, since they spell
    out quite explicitly how to accomplish their task...

    Glenn Andreas                      <gandreas...>
      <http://www.gandreas.com/> wicked fun!
    quadrium2 | build, mutate, evolve, animate  | images, textures,
    fractals, art
  • > More accurately, you'd need to first add the NSTextView(Lines)
    > category to provide the [-lines] functionality (which is found in
    > <http://developer.apple.com/samplecode/TipWrapper/listing7.html>, and
    > does all the dirty work)

      More accurately indeed. :-) I *thought* the -lines method was
    unfamiliar (or new?).

    > So the OP should take another look at those files, since they spell
    > out quite explicitly how to accomplish their task...

      Yes, a closer look is obviously warranted. Of course it's not likely
    to be a ready-made drop-in solution to the problem but it's obvious it
    can be adapted ...

    --
    I.S.
  • Thanks to all. It works perfectly :-)

    On 10/22/07, I. Savant <idiotsavant2005...> wrote:
    >> More accurately, you'd need to first add the NSTextView(Lines)
    >> category to provide the [-lines] functionality (which is found in
    >> <http://developer.apple.com/samplecode/TipWrapper/listing7.html>, and
    >> does all the dirty work)
    >
    > More accurately indeed. :-) I *thought* the -lines method was
    > unfamiliar (or new?).
    >
    >> So the OP should take another look at those files, since they spell
    >> out quite explicitly how to accomplish their task...
    >
    > Yes, a closer look is obviously warranted. Of course it's not likely
    > to be a ready-made drop-in solution to the problem but it's obvious it
    > can be adapted ...
    >
    > --
    > I.S.
    >

    --
    Indie Mac Developer
    web: www.malcom-mac.com
    mail: <malcom.mac...>
    aim: malcombsd
previous month october 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