Professional Web Applications Themes

NSTextView problems plus a few others... - Mac Programming

Hey I am trying to make an NSTextView resize itself to fit the text as the user types. These are the steps I take: NSView *superView; // Not a scrollview; NSTextView *textView = [[NSTextView alloc] initWithFrame:textViewFrame]; [textView setDrawsBackground:NO]; [textView setVerticallyResizable:YES]; [textView setHorizontallyResizable:YES]; [superView addSubview:textView]; ---- Docu says: NSText superclass of NSTextView - (void) setVerticallyResizable: (BOOL) flag Controls whether the receiver changes its height to fit the height of its text. If flag is YES it does; if flag is NO it doesn't. - (void) setHorizontallyResizable: (BOOL) flag Controls whether the receiver changes its width to fit the width of its ...

  1. #1

    Default NSTextView problems plus a few others...

    Hey

    I am trying to make an NSTextView resize itself to fit the text as the
    user types. These are the steps I take:

    NSView *superView; // Not a scrollview;
    NSTextView *textView = [[NSTextView alloc] initWithFrame:textViewFrame];

    [textView setDrawsBackground:NO];

    [textView setVerticallyResizable:YES];
    [textView setHorizontallyResizable:YES];

    [superView addSubview:textView];

    ----

    Docu says:

    NSText superclass of NSTextView

    - (void) setVerticallyResizable: (BOOL) flag
    Controls whether the receiver changes its height to fit the height of
    its text. If flag is YES it does; if flag is NO it doesn't.

    - (void) setHorizontallyResizable: (BOOL) flag
    Controls whether the receiver changes its width to fit the width of its
    text. If flag is YES it does; if flag is NO it doesn't.

    ----

    This doesn't work as I expected, I get two problems:

    1) When the user has written past the textviews width, it doesn't resize
    but wraps. I have tried to find a way to prevent it from wrapping,
    NSParagraphStyle's NSLineBreakByClipping stops it from wrapping, but the
    view still doesn't resize.

    2) The view DOES resize the height as expected. But as it resizes, it
    jumps down (and up) on its superview.

    ----

    Apart from this I have two questions:

    1) How can I get the textview ready for editing immediately after
    placing it on the superview (without the user having to click inside it
    first)? As I write, I'm thinking maybe by making it first responder
    programmatically, but if the answer is something else, please tell me.

    2) How can I make my NSView subclasses respond to mouseclicks
    immediately (mouseDown etc.) across the applications windows, without
    the user first having to click it once to make the window (or is it the
    view) active?

    Sorry about the length.... :-/ I hope you still are reading and want to
    help me... :)

    Per
    Per Bull Holmen Guest

  2. #2

    Default Re: NSTextView problems plus a few others...

    Per Bull Holmen <pbh_news> wrote:
    > Apart from this I have two questions:
    >
    > 1) How can I get the textview ready for editing immediately after
    > placing it on the superview (without the user having to click inside it
    > first)? As I write, I'm thinking maybe by making it first responder
    > programmatically, but if the answer is something else, please tell me.
    >
    > 2) How can I make my NSView subclasses respond to mouseclicks
    > immediately (mouseDown etc.) across the applications windows, without
    > the user first having to click it once to make the window (or is it the
    > view) active?
    OK, I figured out these two:

    1) Make the NSTextView first responder.

    2) Override the views acceptsFirstMouse: to return YES.

    But the main question remains, regarding resizing the NSTextView... is
    it a bug in Cocoa that I have to find a workaround for, or is there
    something I don't know about NSTextView/the text system?

    Per
    Per Bull Holmen Guest

  3. #3

    Default Re: NSTextView problems plus a few others...

    Per Bull Holmen <pbh_news> wrote:
    > I am trying to make an NSTextView resize itself to fit the text as the
    > user types. These are the steps I take:
    >
    > NSView *superView; // Not a scrollview;
    > NSTextView *textView = [[NSTextView alloc] initWithFrame:textViewFrame];
    >
    > [textView setDrawsBackground:NO];
    >
    > [textView setVerticallyResizable:YES];
    > [textView setHorizontallyResizable:YES];
    >
    > [superView addSubview:textView];
    >
    > 1) When the user has written past the textviews width, it doesn't resize
    > but wraps. I have tried to find a way to prevent it from wrapping,
    > NSParagraphStyle's NSLineBreakByClipping stops it from wrapping, but the
    > view still doesn't resize.
    >
    > 2) The view DOES resize the height as expected. But as it resizes, it
    > jumps down (and up) on its superview.
    A textview has a textcontainer; it is the textcontainer that determines
    the width and the wrapping. If you don't want to wrap, make the
    textcontainer very wide and turn off its widthTracksTextView. There is
    an example of how to do this on your hard drive. m.

    --
    matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt/[/url]
    Read TidBITS! It's free and smart. [url]http://www.tidbits.com[/url]
    matt neuburg Guest

  4. #4

    Default Re: NSTextView problems plus a few others...

    matt neuburg <matttidbits.com> wrote:
    > A textview has a textcontainer; it is the textcontainer that determines
    > the width and the wrapping. If you don't want to wrap, make the
    > textcontainer very wide and turn off its widthTracksTextView. There is
    > an example of how to do this on your hard drive. m.
    Thanks a lot.

    In addition to this I had to set the textviews max size to the same size
    as the container. I hoped it would solve the second problem too:
    >> 2) The view DOES resize the height as expected. But as it resizes, it
    >> jumps down (and up) on its superview.
    But it didn't... :(

    I have now "solved" it with the following dirty hack (don't try this at
    home):

    [textView setPostsFrameChangedNotifications:YES];

    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:selector( fixResize: )
    name:NSViewFrameDidChangeNotification
    object:textView];

    textViewUpperBounds = textViewFrame.origin.y +
    textViewFrame.size.height;

    textViewHeight = textViewFrame.size.height;

    ---

    -(void)fixResize:(NSNotification *)note {

    NSRect textViewFrame = [textView frame];

    if( textViewFrame.size.height != textViewHeight ) {

    textViewFrame.origin.y = textViewUpperBounds -
    textViewFrame.size.height;

    [textView setFrame:textViewFrame];

    textViewHeight = textViewFrame.size.height;

    }
    else
    textViewUpperBounds = textViewFrame.origin.y +
    textViewFrame.size.height;

    }

    ---

    This is no good, cause if someone try to both resize and reposition it
    in one operation, it will fail. If anybody has a better solution, or an
    explanation to why the textview jumps up/down as it changes its height,
    please tell me.

    Per
    Per Bull Holmen Guest

  5. #5

    Default Re: NSTextView problems plus a few others...

    Per Bull Holmen <pbh_news> wrote:
    > If anybody has a better solution, or an
    > explanation to why the textview jumps up/down as it changes its height,
    > please tell me.
    Is it really jumping up and down? Remember that the origin is at the
    lower left, not the top. So a change in height will necessarily move the
    top. m.

    --
    matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt/[/url]
    Read TidBITS! It's free and smart. [url]http://www.tidbits.com[/url]
    matt neuburg Guest

  6. #6

    Default Re: NSTextView problems plus a few others...

    matt neuburg <matttidbits.com> wrote:
    > Per Bull Holmen <pbh_news> wrote:
    >
    > > If anybody has a better solution, or an
    > > explanation to why the textview jumps up/down as it changes its height,
    > > please tell me.
    >
    > Is it really jumping up and down? Remember that the origin is at the
    > lower left, not the top. So a change in height will necessarily move the
    > top. m.
    Specifically, what happens is that when the textview increases its
    height, it moves the entire text (and so the frame returned by NSView's
    frame method) down on the superview. It looks pretty stupid. When the
    text is deleted, so the height is decreased, it jumps up again.

    So, when if the frame's size.width gets increased by, say, 15.0, its
    origin.y gets decreased by maybe 30.0.

    The superview is not flipped, and it is a very simple NSView subclass,
    not a scrollview. But the superview itself is placed on a scrollview. It
    barely has any customizations, except from drawRect:

    Per
    Per Bull Holmen Guest

  7. #7

    Default Re: NSTextView problems plus a few others...

    matt neuburg <matttidbits.com> wrote:
    > Per Bull Holmen <pbh_news> wrote:
    >
    > > If anybody has a better solution, or an
    > > explanation to why the textview jumps up/down as it changes its height,
    > > please tell me.
    >
    > Is it really jumping up and down? Remember that the origin is at the
    > lower left, not the top. So a change in height will necessarily move the
    > top. m.
    Specifically, what happens is that when the textview increases its
    height, it moves the entire text (and so the frame returned by NSView's
    frame method) down on the superview. It looks pretty stupid. When the
    text is deleted, so the height is decreased, it jumps up again.

    So, if the frame's size.width gets increased by, say, 15.0, its
    origin.y gets decreased by maybe 30.0.

    The superview is not flipped, and it is a very simple NSView subclass,
    not a scrollview. But the superview itself is placed on a scrollview. It
    barely has any customizations, except from drawRect:

    Per
    Per Bull Holmen Guest

Similar Threads

  1. delegate or subclass NSTextView?
    By None in forum Mac Programming
    Replies: 3
    Last Post: October 1st, 08:58 AM
  2. Replies: 0
    Last Post: September 22nd, 02:18 PM
  3. NSMutableAttributedString in an NSTextView
    By matt neuburg in forum Mac Programming
    Replies: 11
    Last Post: July 26th, 01:21 AM
  4. NSScrollView for NSTextView scrolls then snaps back
    By Harry Mahoney in forum Mac Programming
    Replies: 0
    Last Post: July 16th, 09:19 PM
  5. apt problems
    By Dirk Ouellette in forum Debian
    Replies: 2
    Last Post: July 8th, 04:40 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139