简体   繁体   中英

NSCollectionViewItem, size to content

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM