I'm trying to implement a custom 'NSTextField' that automatically resizes itself while the user insert text.
It's a simple enough task and here is the code I'm using:
- (void)textDidChange:(NSNotification *)aNotification
{
[super textDidChange:aNotification];
[self invalidateIntrinsicContentSize];
}
- (NSSize)intrinsicContentSize
{
if ( ![self.cell wraps] ) {
return [super intrinsicContentSize];
}
NSRect frame = [self frame];
CGFloat width = frame.size.width;
frame.size.height = CGFLOAT_MAX;
NSString *cellTitle = ((NSCell *)self.cell).title;
CGFloat height = [self.cell cellSizeForBounds: frame].height;
return NSMakeSize(width, height);
}
Which does the job. The curiosity is that if I comment the line that calls the NSTextField
's cell title
NSString *cellTitle = ((NSCell *)self.cell).title;
it stops working. It seems that the cell doesn't update its title until requested. Of course if the cell's title doesn't change the textfield can't resize itself when the user type text.
Is this behaviour a feature or a bug?
As I said the code above does the job so this is more of a curiosity. The only annoyance is that the line produces a warning and I would like to know if there is a better way to do this.
Edit: Never mind I figured out the if I change the line to [(NSCell *)self.cell title];
the compiler stops complaining. The curiosity about feature or bug still stands though.