The following uses an NSTextView subclass that must be created in code. For reasons of its own Xcode won't allow you to instantiate an NSTextView in a nib without an enclosing NSScrollView instance.
This class lets the only defines the text view intrinsic height - the width is left undefined which allows the view to grow with its enclosing view. I used this in an NSStackView
and it seemed to work well. Trying to bludgeon NSTextField so that it could wrap multiline text, edit and support Auto Layout was too messy.
Note we have support for a focus ring as I wanted my class to act like and Uber text field. Also note that we have no support for a border. In my actual usage I create a compound view that wraps the custom text view. This wrapper view draws a border as required.
@interface BPTextViewField : NSTextView
// primitives
@property (assign, nonatomic) CGFloat borderOffsetX;
@property (assign, nonatomic) CGFloat borderOffsetY;
@end
@implementation BPTextViewField
#pragma mark -
#pragma mark Life cycle
- (instancetype)initWithFrame:(NSRect)frameRect textContainer:(nullable NSTextContainer *)container
{
self = [super initWithFrame:frameRect textContainer:container];
if (self) {
[self commonInit];
}
return self;
}
- (nullable instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit
{
_borderOffsetX = 1;
_borderOffsetY = 3;
self.usesFontPanel = NO;
self.usesFindPanel = NO;
}
#pragma mark -
#pragma mark Auto layout
- (NSSize)intrinsicContentSize
{
NSTextContainer* textContainer = [self textContainer];
NSLayoutManager* layoutManager = [self layoutManager];
[layoutManager ensureLayoutForTextContainer: textContainer];
NSSize size = [layoutManager usedRectForTextContainer: textContainer].size;
return NSMakeSize(NSViewNoIntrinsicMetric, size.height);
}
#pragma mark -
#pragma mark Accessors
- (void)setString:(NSString *)string
{
[super setString:string];
[self invalidateIntrinsicContentSize];
}
#pragma mark -
#pragma mark Text change notifications
- (void)didChangeText
{
[super didChangeText];
[self invalidateIntrinsicContentSize];
}
#pragma mark -
#pragma mark Drawing
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
}
#pragma mark -
#pragma mark Focus ring
- (void)drawFocusRingMask
{
if (self.editable) {
NSRectFill(self.focusRingMaskBounds);
}
}
- (NSRect)focusRingMaskBounds {
NSRect r = [self bounds];
return NSMakeRect(r.origin.x - self.borderOffsetX, r.origin.y - self.borderOffsetY, r.size.width + self.borderOffsetX * 2, r.size.height + self.borderOffsetY * 2);
}
@end