有没有办法自动调整大小height使用约束的 NSTokenField (保持宽度恒定)?
-sizeToFit
应该有效,但事实并非如此。如果我设置一个约束来保持宽度不变并调用此方法,它将忽略约束并仅调整宽度大小(当我想要的是仅调整高度大小时)。
基于如何让 NSTextField 在自动布局中随着文本增长? https://stackoverflow.com/questions/10463680/how-to-let-nstextfield-grow-with-the-text-in-auto-layout
也不要设置大小限制,顺其自然。
方法intrinsicContentSize
in NSView
返回视图本身认为的其内在内容大小。
NSTextField
计算此值时不考虑wraps
其单元格的属性,因此如果放置在单行上,它将报告文本的尺寸。
因此,一个自定义子类NSTokenField
可以重写此方法以返回更好的值,例如单元格提供的值cellSizeForBounds:
method:
-(NSSize)intrinsicContentSize
{
if ( ![self.cell wraps] ) {
return [super intrinsicContentSize];
}
NSRect frame = [self frame];
CGFloat width = frame.size.width;
// Make the frame very high, while keeping the width
frame.size.height = CGFLOAT_MAX;
// Calculate new height within the frame
// with practically infinite height.
CGFloat height = [self.cell cellSizeForBounds: frame].height;
return NSMakeSize(width, height);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)