Basically, I have an NSTableView with 1 collumn, and I\'m inserting long strings into each row. However, not all the strings are long, so I\'d like the height of each row to
Here is another solution, which works well in my case:
Objective-C:
- (double)tableView:(NSTableView *)tableView heightOfRow:(long)row
{
if (tableView == self.tableViewTodo)
{
CKRecord *record = [self.arrayTodoItemsFiltered objectAtIndex:row];
NSString *text = record[@"title"];
double someWidth = self.tableViewTodo.frame.size.width;
NSFont *font = [NSFont fontWithName:@"Palatino-Roman" size:13.0];
NSDictionary *attrsDictionary =
[NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
NSAttributedString *attrString =
[[NSAttributedString alloc] initWithString:text
attributes:attrsDictionary];
NSRect frame = NSMakeRect(0, 0, someWidth, MAXFLOAT);
NSTextView *tv = [[NSTextView alloc] initWithFrame:frame];
[[tv textStorage] setAttributedString:attrString];
[tv setHorizontallyResizable:NO];
[tv sizeToFit];
double height = tv.frame.size.height + 20;
return height;
}
else
{
return 18;
}
}
Swift:
func tableView(tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
if let log:Log = logsArrayController.arrangedObjects.objectAtIndex(row) as? Log {
if let string: String = log.message! {
let someWidth: CGFloat = tableView.frame.size.width
let stringAttributes = [NSFontAttributeName: NSFont.systemFontOfSize(12)] //change to font/size u are using
let attrString: NSAttributedString = NSAttributedString(string: string, attributes: stringAttributes)
let frame: NSRect = NSMakeRect(0, 0, someWidth, CGFloat.max)
let tv: NSTextView = NSTextView(frame: frame)
tv.textStorage?.setAttributedString(attrString)
tv.horizontallyResizable = false
tv.sizeToFit()
let height: CGFloat = tv.frame.size.height + 20 // + other objects...
return height
}
}
return 100 //Fail
}