NSTextView with a Background Image

Discussion in 'Mac Programming' started by Littleodie914, Oct 8, 2007.

  1. Littleodie914 macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #1
    Hey all, bit of a stumper here. I'm attempting to draw a background behind an NSTextView, and I've run into a problem. I currently have an NSTextView as a subview of a NSView, the NSView drawing the background image. I set the NSTextView to *not* draw a background, and it looks great!

    Until, that is, I enter more text. It "trails," and I understand the problem to be something about setting the ?NSClipView?'s copyWhileScrolling value to false, unavoidably, when setting the TextView to not draw a background. I've tried everything, from different background combinations, trying to redraw views on text changes, setting opacity values, and I can't find a solution.

    Is there any *proper* way to give an NSTextView a background image? :)
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    One way I did this before was to subclass NSTextView and make it transparent:

    Code:
    - (void)awakeFromNib
    {
    	[self setDrawsBackground:NO];
    	[(NSScrollView *)[self superview] setDrawsBackground:NO];
    }
    
    - (BOOL)isOpaque
    {
    	return NO;
    }
    
    Then I put a custom NSView, which draws the image, behind the text view. It worked great. I'm sure there is a better way to do this though (this is old code).

    You can see an example of this in the demo of a commercial product I developed.
     
  3. Littleodie914 thread starter macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #3
    I'm on my laptop right now, so I don't have my project with me, but I was just double-checking; the method you used didn't produce any weird text trailing effects?
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    I had the image view redraw whenever the text view was scrolled or had its bounds changed.

    Actually after looking at the code, I had the text view as a subview within the image view.
     
  5. Littleodie914 thread starter macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #5
    Got it working, turns out I was calling setDrawsBackground: on my subclassed NSView. Problem is, that method isn't implemented until later on down the hierarchy tree, so my subclass had no idea what I was asking it to do. :)

    Strange though, it never displayed a warning, only an issue in the console, and the program continued to work fine!

    Thanks for your help!
     
  6. sabharwal81 macrumors newbie

    Joined:
    Aug 25, 2009
    #6
    but how do i set a NSView as a subview of NSImageView?
     

Share This Page