Question

Can I make an multi-line-NSTextField object auto-resize by its content?

For example, I want to make the object's width fixed, but its height fit its content. How should I generate the layout constraints then?

Was it helpful?

Solution 2

Yes, You can use NS(Attributed)String+Geometrics category.

NS(Attributed)String+Geometrics is a pair or reuseable categories (on NSString and NSAttributedString) which you can add to your projects, providing simple methods to get the width and height required for text drawing.

http://www.sheepsystems.com/sourceCode/sourceStringGeometrics.html

OTHER TIPS

Follow the below steps:-

1) Select the NSTextField then inside interface builder->AttributeIsnpector->set control to LineBreak-word wrap-> set state-> Enabled

2) Implement below code :-

-(void)controlTextDidChange:(NSNotification *)obj
{
   //if you want to width to change then uncomment below
    //    CGFloat width=[[self.txtFld cell] cellSizeForBounds:self.txtFld.bounds].width;
        CGFloat heigth=[[self.txtFld cell] cellSizeForBounds:self.txtFld.bounds].height;
        [self.txtFld setFrameSize:NSMakeSize(160, heigth+10)];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top