Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextView and NSTextContainer size & clipping area

Tags:

cocoa

I found very interesting behavior of text container inside NSTextView. When i set size of container so it less than size of NSTextView frame and try draw any figures (like lines, rectangles) in NSTextView drawRect: , all my figures clipped to size of text container.

So, frame size of NSTextView "allows" me to use it for drawing, but seems that is limited to container size.

If there are any possibility to draw inside text view but outside of text container ?

Code in Custom NSTextView - (void) drawRect:

[super drawRect:dirtyRect];

NSBezierPath* aPath = [NSBezierPath bezierPath];
[aPath moveToPoint:NSMakePoint(100, 100)];
[aPath lineToPoint:NSMakePoint(500, 100)];
[aPath stroke];

Custom textview resize policy set, so it resizes in all dimensions with container. This is code for custom NSTextView

- (void) setFrameSize:(NSSize)newSize {

    [super setFrameSize:newSize];

    NSTextContainer *container = [self textContainer];
    newSize.width -= 200;
    [container setContainerSize:newSize];
}
like image 415
dmitrynikolaev Avatar asked May 07 '11 07:05

dmitrynikolaev


1 Answers

Thanks Ross Carter for advice:

Try wrapping the call to super like this:

[NSGraphicsContext saveGraphicsState];
[super drawRect:rect];
[NSGraphicsContext restoreGraphicsState];
like image 190
dmitrynikolaev Avatar answered Sep 30 '22 13:09

dmitrynikolaev