Question

I have an NSCollectionView, which I'm binding to a collection of strings.

The view that I'm using for my NSCollectionViewItem is very simple, it simply contains an NSTextField.

NSView - NSTextField

The length of the strings is variable. Is there any way to adjust the size of my text field / view to account for the length of the strings?

For smaller strings, the text is nicely centered in the view. But for longer strings, the text gets truncated.

If it helps, all of the strings within the collection will be approximately the same length.

Was it helpful?

Solution

The way that I solved this was quite simple. The strings in my NSCollectionView can be of different lengths - preventing me from creating the view of the correct size up front; but within a collection all strings will be the same length.

I create an NSAttributedString of the correct length and font style, then get it's Size property. Using that, I can set the MinItemSize on my NSCollectionView appropriately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top