Xcode Label resizes View - macos

Imagine a NSView which contains a NSLabel and constraints to leading >= 10 and trailing >= 10. Now when I change the StringContent, the NSLabel autoresizes (like it should) but it also resizes the whole NSView.
Here is a screenshot of Xcode
The window on startup:
And the window after the Stringvalue has changed:
Note that I cannot shrink the window to a smaller size than the labels
Is there a way to stop the NSLabel resizing the NSView without setting a maxwidth?
So maybe the StringValue would be "This is an extreme long text I want to dis..." and when resizing the NSView, the Label resizes with it and will show the whole StringValue
Thanks in advance for your help!
Solution
The key was setting the Content Compression Resistance Priority to a lower than standard, in my case I set it to 500

Please try selecting your label and setting it's horizontal content hugging priority to something less. Here's how it looks:

Related

How to fix NSTextView's weird sizing inside a magnified NSScrollView

I have an NSTextView inside NSScrollView/NSClipView, the usual setup. When I magnify the scroll view with [NSScrollView setMagnification:...] and resize the window, the width of text view's frame gets constantly larger, regardless of whether I stretch or shrink the window.
If the scroll view is not magnified, text view behaves normally. I have tried removing constraints and disabled subview autoresizing, but nothing helps. Whenever i set any sort of magnification, text view size changes on every call to resize. If the magnification is under 1, it shrinks.
Any bugs in TextContainer shouldn't make it wider either, as I've set textContainer.widthTracksTextView = false;
I am trying to keep the textContainer centered in my NSTextView by setting insets to it, but it gets impossible with the varying sizes. I've gone through my code and nothing should make it resize. Is this a bug or does the setMagnify: cause problems with constraints or some other math in LayoutManager?
For anyone trying to figure this out:
This is caused by a minimum width constraint for the magnified view, or any of its subviews. Seems like a bug, as I can't wrap my head around how this would be intended behaviour.
Just remove the >= constraint and do something to limit the size in run-time if needed.

Set constraints in Xcode so NSView will resize if content width increases

I have a NSView which contains a NSTextField.
I want to set constraints so that the view width will increase if the NSTextField content reaches the view size.
When setting a leading constraint the NSTextField width will increase if the text contains longer text but I cannot get the NSView to resize.
What do I miss here?
Thanks!
Yo need to set a constraint for the vertical axis and if you want to keep the text inside the box, you need to set the trailing constraint.
Hope this helps you :)

NSTextField sizeToFit or frame update shift text to left

I have a strange issue with sizeToFit method on a NSTextField.
I have a NSView where I'm creating a CALayer and a NSTextField. I use the sizeToFit method to resize my CALayer according to the field value. It works well, but when I'm inserting a space, the text shifts on the left inside the field's frame (see image below).
Both layer's and text field's frames are positioned well (origin isn't moving).
Any clue on this would be highly appreciated.
EDIT
Frame update has the same behavior, even when not using sizeToFit.
Sometimes it's shifting when inserting a space, or sometimes it's coming back to its wanted position when inserting a space.
The underlying layer doesn't move from its origin.
EDIT
It looks like the container of the NSTextField needs enough space to display.
What I discovered is that it needs actually a lot of space.
We can't access the container of a NSTextField, so I add a large value to the width of the string (depending on the size of the font) before calling a frame update or [self sizeThatFits:stringSize]; and [self sizeToFit];.
I think there's no need in -sizeToFit-method. It's automatically called when layout performs. If you need to adjust size of your NSTextField-instance, subclass it and override -intrinsicContentSize-method.
I used this approach with "fade out"-text mask at the end of the text field. Using autolayout and intrinsic content size my textfields were always with that mask even if on the parent view was enough space for layout.
- (CGSize)intrinsicContentSize
{
CGSize size=[super intrinsicContentSize];
// adjust size somehow (or may be not)
return(size);
}
Each time you need to "update" text field size (while typing?), call the -invalidateIntrinsicContentSize.

Auto-layout issue with UIButton with long title that compresses an aligned Segmented control which should have fixed width and should never compress

In my Xcode 6.2 Swift project I have an issue with Auto-layout (I'm also using size classes) that I'm not able to figure out...
In Main.storyboard I have a view containing, among the others interface elements, a UIButton and a UISegmentedControl that are positioned at the same height on the opposite side of the view.
I'm setting manually all the constraints in Interface Builder (none in code) and my every view is working just fine, except in this case (and this particular issue only occurs when I have a long text).
The button is aligned to the left border of the view and its constraints are:
Leading space to superview == 0
Trailing space to segmented controller >= 8
Top and bottom space to other interface elements == 8
The segmented controller (which has 2 segments) is aligned to the right border of the view and its constraints are:
Trailing space to superview == 0
Leading space to button >= 8
Top and bottom space to other interface elements == 8
The button in the storyboard has a title "Some title", but actually the actual title is always set in code in ViewWillAppear:
myButton.setTitle(aStringThatSometimesIsPrettyLong, forState: .Normal)
The visual result I need to achieve (on every possibile device and orientation) is that the Button title I set in code, while it can be displayed in good length in the interface, should never compromise the size of the segmented control, compressing the labels of the two segments.
So, I want the size of the segmented control to be fixed and I'm willing to accept the fact that the Button title, if long, can be truncated with dots.
Instead, no matter what I try (and I'll explain what I've tried in a moment) when the Button title is very long it is not truncated, instead the segmented control is compressed and therefore its two segments labels are truncated.
So far, I've tried, separately and together, these steps:
Adding a width minimum constraint to the segmented control.
Incremented (in steps, up to 1000) the Content compression resistance of the segmented control while decreasing the correspondent value of the button.
Increased (up to 1000) the Content hugging priority of the button.
I think I can't set a maximum width to the Button, because it can stretch depending on the title set in code and, more important, on what device the app is run on.
My biggest issue is that, no matter what I try, when I run the app I always get the same behavior (button title completely shown, un-truncated, and compressed segmented control). It seems like adding these constraints doesn't change anything, and it never happened to me before with Auto-layout... messing up, a lot, but no change adding constraints, this is new!
Maybe the issue is that the button title is set in ViewWillAppear and not in the Storyboard, but my app wouldn't work properly if I couldn't set its title in code.
Last, but pretty important, I have to admit that, while I've managed so far to get Auto-layout and Size classes working on all devices and orientations for all the (over 10) viewcontrollers of my app, I've actually never written a single line of code for Auto.layout and Size classes: I've done everything in Interface Builder and, if possible, I'd really love to continue this way.
Any suggestion would be really, really appreciated!
Thanks in advance,
Cesare
As #KenThomases pointed out, the constraints I was setting were actually right (actually, it also works with less constraints now that, thanks to his answer, I figured out the issue), but the Segmented control wasn't getting its intrinsicContentSize. Fixed that, now everything's fine.

How to resize NSTextField dynamically?

I want an NSTextField to get bigger as the user types, and smaller as backspace is pressed etc.
The only thing I've found is [textField sizeToFit] but that doesn't run dynamically, it's just a one off resizing. Is there a property or something I need to set?
p.s. I only need it to resize horizontally (single line text).
What Richard said, except you need to know by how much to resize your textfield. To get this value, use the the layout manager of the NSTextField and call something like boundingRectForGlyphRange:inTextContainer: on it with a range of all glyphs. This should tell you the dimensions of the rectangle bounding all of your text. Take these dimensions and set them to the width and height of your NSTextField content.
Set a delegate on the NSTextField which implements
- (void)controlTextDidChange:(NSNotification *)aNotification
Every time the delegate gets called resize the control per your idea above.

Resources