Objective-c – Giving an NSTextView some padding/a margin

cocoaobjective c

How would I give a NSTextView some padding/a margin to the left?
I know how you do it in a NSTextField (by subclassing NSTextFieldCell) but how do you do it in a NSTextView?

EDIT: A bit more info:
1. The Text View just has plain text no rich text and no other fancy stuff like a proper text editor (e.g Paragraph insets).
2. Is it possible to use setTextContainerInset: for this?

Best Answer

You could try subclassing NSTextView and override the textContainerOrigin.

Details here.

For example this subclass will give a top and bottom margin of 5 left of 20 and right of 10.

@implementation MyTextView

- (void)awakeFromNib {
    [super setTextContainerInset:NSMakeSize(15.0f, 5.0f)];
}


- (NSPoint)textContainerOrigin {
    NSPoint origin = [super textContainerOrigin];
    NSPoint newOrigin = NSMakePoint(origin.x + 5.0f, origin.y);
    return newOrigin;
}

@end
Related Topic