I have a view-based NSTableView and NSTableCellView subclass which having multiline NSTextField with word-wrap enabled. Now I want my table to have dynamic row height according to textfield content. Autolayout is also enabled.
Textfield's constraints is shown in image:
Tableview heightForRow method code :
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row {
VerseTableCell *cell = [self getTableCellForRow:row];
[cell.txtSuraDetail invalidateIntrinsicContentSize];
[cell invalidateIntrinsicContentSize];
[cell setTranslatesAutoresizingMaskIntoConstraints:YES];
[cell layout];
[cell setNeedsDisplay:YES];
[cell setNeedsUpdateConstraints:YES];
[cell setNeedsLayout:YES];
[cell layoutSubtreeIfNeeded];
NSRect size = cell.txtSuraDetail.frame;
CGFloat height = (size.size.height) + 30;
return height;
}
Cell Layout Method :
- (void)layout {
[super layout];
self.txtSuraDetail.preferredMaxLayoutWidth = self.txtSuraDetail.frame.size.width;
}
I've tried the following links but nothing helped, textfield's text is still cutting.
.usesAutomaticRowHeights
in macOS 10.13 as described here: developer.apple.com/library/content/releasenotes/AppKit/… (In the section titled NSTableView Automatic Row Heights). – DentonNSTextField
and remove the height constraint. I believe you also have to set the number of lines in the field to0
. – Denton