Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Giving an NSTextView some padding/a margin

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?

like image 212
Joshua Avatar asked Dec 23 '09 07:12

Joshua


2 Answers

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
like image 75
Abizern Avatar answered Sep 17 '22 12:09

Abizern


Just to add an update to this. iOS7 adds a property to UITextView called textContainerInset. Calling setTextContainerInset will create margins inside the TextView for the content.

like image 44
George Avatar answered Sep 17 '22 12:09

George