문제

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?

도움이 되었습니까?

해결책 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

다른 팁

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)];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top