views:

220

answers:

1

Hi All

I am subclassing NSTextView and over-riding the drawRect method in order to draw an NSBezierPathWithRoundedRect around the textview, however - as it has rounded edges, they interfere with the text in the text view.

Is there any way to apply some kind of margin or padding all around the text input area of the NSTextView so that it is more inset, and away from the rounded edges? Or is a better approach to sit the NSTextView within an NSView and apply the rounded stroke to the NSView instead?

- (void)drawRect:(NSRect)dirtyRect {
// Drawing code here.
[super drawRect:dirtyRect];

NSRect rect = [self bounds];
NSRect newRect = NSMakeRect(rect.origin.x+2, rect.origin.y+2, rect.size.width-3, rect.size.height-3);

NSBezierPath *textViewSurround = [NSBezierPath bezierPathWithRoundedRect:newRect xRadius:10 yRadius:10];
[textViewSurround setLineWidth:2.0];
[[NSColor whiteColor] set];
[textViewSurround stroke];

}
A: 

I think -setTextContainerInset: does what you need. Alternatively, you should not call super in your implementation of -drawRect: and draw the text container yourself.

Costique
The inset only does a left hand side inset unfortunately, its not all the way round. I will try drawing the container myself.
mootymoots
It's still not possible to position the NSTextContainer. I can only assign width and height to it, so I assume it is positioned at 0,0?
mootymoots
The docs say: "Sets the empty space the receiver leaves around its associated text container", so it looks like the NSSize determines symmetrical horizontal and vertical insets, but you may have to set both the container's inset (setTextContainerInset: method of NSTextView) and size (setContainerSize: method of NSTextContainer) yourself.
Costique