如何通过编程方式按内容自动调整标签(NSTextField)?

And*_*ang 3 macos cocoa nstextfield autolayout

我可以通过其内容自动调整多行NSTextField对象吗?

例如,我想使对象的宽度固定,但其高度适合其内容.那我该如何生成布局约束呢?

Hus*_*bir 6

请按照以下步骤操作: -

1)选择NSTextField里面的内容interface builder->AttributeIsnpector->set control to LineBreak-word wrap-> set state-> Enabled

2)实施以下代码: -

-(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)];
}
Run Code Online (Sandbox Code Playgroud)