我有一个里面有NSTextField的NSView。问题是,每次在显示视图之前,NSTextField的大小都可以改变,我希望NSView的大小也随之改变。因此,如果文本字段有2行长,nsview就会很小(刚好围绕在文本字段周围),但当它有10行长时,我不希望视图的大小与文本字段相隔,我希望视图与之一起增长。
我该怎么做呢?谢谢。
发布于 2011-04-09 16:36:15
这是我在我的表视图中使用的,但它应该同样适用于您的情况:
float textLabelWidth = 190;
NSString *Text = [[deals objectAtIndex:[indexPath section]] objectForKey:@"title"];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:13.0];
CGSize constraintSize = CGSizeMake(textLabelWidth, MAXFLOAT);
CGSize labelSize = [Text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 20; // <--- this is what your NSView's height should be, just edit the textLabelWidth and padding (in this case 20) to whatever you desire.希望这能有所帮助!
发布于 2016-01-24 04:22:24
您需要设置"contentHugging和contentCompression“优先级。
简而言之:
contentHugging: sets the preference where a control resists being made *larger* than it's intrinsic size
contentCompression: sets the preference where a control resists being made *smaller* than it's intrinsic sizeApple在这里有一个推荐人:
特别是向下滚动到Setting Content-Hugging and Compression-Resistance Priorities部分。
这就是处理textFields等问题的方法。等等。
https://stackoverflow.com/questions/5603548
复制相似问题