0
我有多行NSTexField,用於顯示靜態文本,每個窗口顯示的長度可能不同。此NSTextField具有高度限制,將其限制爲兩行。NSTextField(多行)在填充所有行後水平增長
我想設置行爲,NSTextField只會水平增長,只會填滿那兩條水平線。 現在,當我將水平壓縮阻力設置得更高時,文本字段寬度會變得非常大,以便在一行上顯示所有文本。這隻能通過使用自動佈局約束來實現,還是必須以某種方式計算nstextfield的寬度?
我試着在superview中重載「layout」方法。但它沒有按照我的預期工作。
- (void)layout
{
[self solveLayoutForView:self];
}
- (void)solveLayoutForView:(NSView*)view
{
for (NSView* subView in [view subviews])
{
if (subView.subviews.count)
[self solveLayoutForView:subView];
else
{
NSLayoutConstraint* height = [subView constraintForAttribute:NSLayoutAttributeHeight];
if (height && height.constant > 17 && [subView isKindOfClass:[NSTextField class]]) // seems like multiline nstextfield
{
NSTextField* textField = (NSTextField*)subView;
textField.preferredMaxLayoutWidth = 0;
[super layout];
textField.preferredMaxLayoutWidth = NSWidth([textField alignmentRectForFrame:textField.frame]);
[super layout];
}
}
}
}