PDA

View Full Version : NSTextView with a Background Image




Littleodie914
Oct 8, 2007, 06:44 AM
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? :)



kainjow
Oct 8, 2007, 09:45 AM
One way I did this before was to subclass NSTextView and make it transparent:


- (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 (http://www.talkingpanda.com/products/demos/iLingo.dmg) of a commercial product I developed.

Littleodie914
Oct 8, 2007, 01:11 PM
One way I did this before was to subclass NSTextView and make it transparent:


- (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 (http://www.talkingpanda.com/products/demos/iLingo.dmg) of a commercial product I developed.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?

kainjow
Oct 8, 2007, 01:19 PM
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.

Littleodie914
Oct 8, 2007, 02:19 PM
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!

sabharwal81
Jul 21, 2010, 12:39 AM
but how do i set a NSView as a subview of NSImageView?