How to make NSStackView truncate right-most NSTextField - cocoa

I have one line in my UI, that consists of fixed-sized string (FS), fixed-sized image and variable-sized string (VS). I need this line to be centered in its superview, possibly truncating the VS if it no longer fits in. Just like simple centered NSTextField with truncate enabled would do. But NSTextField can't hold an NSImage AFAIK.
I suppose NSStackView should be able to do this. I have set it up to be centered in its superview and have an NSTextField, NSImage and NSTextField inside. But when I set VS to something very long, both FS and VS are pushed out of superview bounds and clipped.
Is there a way to tell NSStackView to never move FS out of superview bounds and rather truncate VS? Or is there any other technique to achieve my goal?
What I have tried so far:
Use NSStackView and set compressionResistance to 499 on the VS - no effect
Use NSTextView - could not make it truncate text and fit on single line. Also seems like an overkill
Use NSView - it requires me to set x-position so it is not properly
centered.
I could use NSView and calculate sizes of the strings and modify constraints to make this work. But I think there must be a better way.
Thanks for any ideas,
J.

It sounds like you haven't constrained the stack view to fit within its container. In addition to the centering constraint, you need to set its leading to be greater than or equal to the leading of the superview and its trailing to be less than or equal to the trailing of its superview.
The compression resistance of FS and the image view should be higher than the compression resistance of VS, and could even be required to prevent those two from ever being compressed.
The horizontal clipping resistance of the stack view should be higher than the compression resistance of VS. It's fine to leave it at required. You want VS to be compressed rather than clipped.
What the compression resistance of VS should actually be depends on the rest of your view hierarchy and constraints. It should presumably be less than 490 (NSLayoutPriorityDragThatCannotResizeWindow) so it doesn't force the window to resize.

Related

Xcode auto alignment and constraints - equal distances between several images

I have nine images ad I want them to display in a single column on the device. The images should retain their original height and width (that I will set) and the only thing that should change according to the screen height should be the distance between the images.
I'm not really sure how to approach this problem - I tried using a container, but it didn't turn out as I expected. Should I set the constraints programmatically? And if so, how? What should the constraints be if I want the images to be from top to bottom of he screen?
Consider using a stack view:
On iOS, UIStackView provides UIStackViewDistributionEqualSpacing, which I believe will arrange images as you want
On Mac, NSStackView can automatically space images as you describe if the individual image views are all in the same gravity and retain their sizes at a high enough priority (using constraints or content hugging priority)

Autolayout - equal distribution of 6 views

I want to have 6 objects (buttons) laid out inside one view. They should, however, follow some constraints:
Two top buttons should have the same vertical distance from superview (A)
Two bottom - the same (C)
Two in the middle should have their centers at the superview's center line
The vertical distances between all buttons (E) should be the same
and last but not least - the buttons should be square (so the width and height should be the same)
A = C
B = D
Is it possible to have this effect just in the IB, or should I use some additional code for the constraints?
This is a logical request, but constraints are defined using the attributes of views, but cannot not be defined in relation to other constraints. That having been said, there are a number of approaches:
Layout guides: An approach which doesn't require predetermining the any spacing is to have UILayoutGuide objects or, if using iOS versions before 9, just use hidden views, i.e. views with clear background or alpha of zero, in between the buttons.
The idea is to add these layout guides with addLayoutGuide (or add invisible views with addSubview if supporting iOS versions predating iOS 9) in between your six buttons as "spacers", and define the spacers to be the same size as each other, and with constraints between the spacers, the superview, and the buttons that will go in between the spacer. Once you lay that out (showing the horizontal spacer views in blue, vertical ones in red, just so you can see them):
The equivalent VFL for the constraints for those red UIView objects, called vspacerX, would be:
H:|[vspacer1][button1(100)][vspacer2(==vspacer1)][button2(==button1)][vspacer3(==vspacer1)]|
H:|[vspacer1][button3(==button1)][vspacer2][button4(==button1)][vspacer3]|
H:|[vspacer1][button5(==button1)][vspacer2][button6(==button1)][vspacer3]|
And constraints on the blue UIView objects, called hspacerX, like:
V:|[hspacer1][button1(100)][hspacer2(==hspacer1)][button3(==button1)][hspacer3(==hspacer1)][button5(==button1)][hspacer4(==hspacer1)]|
V:|[hspacer1][button2(==button1)][hspacer2][button4(==button1)][hspacer3][button6(==button1)][hspacer4]|
You don't have to use VFL to define these constraints, as any way you define these constraints will work, but it's just a concise format for describing the collection of constraints that I employed.
Anyway, when the view is rendered with those layout guides (or invisible views), it yields evenly spaced buttons like so:
Another approach is to have six "container" views, that would look like:
The equivalent VFL for these six container UIView objects might look like:
H:|[container1][container2(==container1)]|
H:|[container3(==container1)][container4(==container1)]|
H:|[container5(==container1)][container6(==container1)]|
V:|[container1][container3(==container1)][container5(==container1)]|
V:|[container2(==container1)][container4(==container1)][container6(==container1)]|
You can then add your buttons to that, centering one on each of the six little containers and then make your containers clear:
This works, too, but just a slightly different spacing (where the margins are half of the spacing between the views, whereas the other approach keeps the margins the same as the spacing between them.
Stack view: In a permutation of the prior point, in iOS 9, you can also use UIStackView, designed precisely for evenly spacing views. In this case, put two buttons each in three horizontal stack views, and then place those stack views within a vertical stack view. This achieves six evenly sized container views.
See WWDC 2015 video What's New in Cocoa Touch.
The problem with stack views is that they can be used to ensure even spacing between the arranged subviews, they don't ensure spacing before the first arranged view nor after the last arranged view. So, the kludge to get around that is to, for horizontal stack view, include two more zero width views (or zero height for vertical stack views). Then when you use even spacing on the stack view, it also give you what will appear to be spacing before and after all of the arranged subviews.
NSLayoutAttributeCenterX with multiple: Another technique involves defining the attribute:NSLayoutAttributeCenterX and attribute:NSLayoutAttributeCenterY attributes for your six buttons, but rather than using the constant values, use the multiplier field. This technique enjoys a little simplicity, but doesn't always render the desired effect, so I won't describe it unless it's something you definitely want to pursue. I've already entered tl:dr territory here.
Collection view: Another approach is to use a UICollectionView, which handles this scenario gracefully. It's well designed to let you layout cells in a grid.
Hardcoding values: For the sake of completeness, I'll note that you could simply specify specific values for A, B, C, and D (as well as the width and height constraints). You don't even have to worry about setting the E constraints, but rather just set the vertical center constraint of the middle two to their superview, and you're effectively done (because the spacing represented by E should be a natural result of the previous steps, assuming A=C and B=D). If you want to adjust these values on the basis of device size and/or orientation, you can then implement a viewWillLayoutSubviews to adjust the constants for these constraints according to the size of the view.
Update: I have a better solution that does not use spacers. Check it out here.
Ok, this can be achieved very quickly in IB. It's so so simple. Here's a diagram that will help illustrate.
Assume v1-6 are your buttons, and s1-5 are your spacers.
1) in IB control drag out all of the connections shown by the red lines.
2) shift click v1-6 and pin icon (looks like |-I-| ) set the width and height to a definite value. also, set the height and width to be equal.
3) shift select s1-4 (not 5) and set the height to equal. do not give it a definite height, since this should be calculated by the system. you might also need to set the widths of s1-4 to be equal, but don't give them a definite width.
4) control drag from the centre views to the leading and trailing edge and set the centre constraint.
So, you might think, ok, this should work now. It doesn't. Here's my app running in portrait with slightly different colors. Looks good. (Notice, you would make the spacers invisible once you get it setup).
But when I rotate, oops!
What's happening here? The problem is incredibly easy to solve once we understand what's gone wrong. What we want is for IB to not shrink our views. We want IB to make the spacers and the spaces to shrink and grow as necessary, but to leave our views alone. Basically, IB has shrunk the spacers down as far as it can in portrait and to attempt to make everything fit IB has shrunk our views. But we wanted IB to shrink the vertical spaces between views and spacers, not our views. The solution is so easy. All we have to do is adjust the priority of the vertical spaces and all is well. So, select the vertical spaces in IB and adjust the priority to 750. The vertical spacing lines will show as dashed. Done.
Ok, so here's everything as we expect it.
And with the spacers made clear:

Visual Format Language - padding area

I have several objects that are static in size that hug the top and bottom of the screen. However, there is a blank area in the middle of the screen that I would like to stretch with orientation change.
I have solved an issue by adding an extra clear view [paddedView] that can be stretched but wanted to know if there was an easier way to do this without the paddedView just with Visual Format Language.
#"V:|-20-[topLabel(40)]-15-[anotherTopLabel(40)]-[paddedView]-[bottomView(73)]|";
Instead of a view used only for padding, you can either specify a lower priority to a distance or specify the constraint as "greater than or equal to".
Since what you want is a flexible space, the last option sounds like the best one:
#"V:|-20-[topLabel(40)]-15-[anotherTopLabel(40)]-(>=15)-[bottomView(73)]|"
The number 15 is of course just an example.

Cocoa control for lists?

Is there any Cocoa control equivalent to WPF's itemscontrol? I have been using a single column, view based NSTableView but can't seem to get individual rows to size to fit their content, or even to get different height rows (all views are sized to be as tall as the tallest one).
More generally speaking, is there any way of getting views to size to fit their content? I have been using constraints, but seem to only be able to make views size according to their container's size.
You should use NSTableViewDelegate and it's -tableView:heightOfRow: method to specify the individual row height.

gtk: expand widget as if it had some text in it

I have a GTK widget, in this case, a TreeView. It starts off pretty small and compressed, as there's no text in it besides the columns names. As I add things, it grows horizontally to cover the text and vertically to cover the extra rows. If I then take those away, it retains its expanded size.
It's kind of annoying for your window to always be resizing as you add things. My question is - how can I "pre-"size the widget? Like one way would be to fill it with junk text that I think is the biggest size it can get, and then remove the text, but that won't look very pretty. Is there a better way?
Simplest answer: treeview.set_size_request(width, height) (but then your tree view won't grow when it needs to afterwards.)
Is your tree view in a box? Have you tried packing it with expand=True and fill=True?
window.set_default_size(width, height) on your whole gtk.Window is the best solution, because that "pre-"sizes the window as you say. It can still grow, or be resized smaller by the user.

Resources