Button with varying localized title with auto layout - macos

I am looking into taking the plunge and use Auto Layout on an existing app. The interface has a button on the main window. The button changes its title depending on the state of the app. I plan to localize the app.
Since Auto Layout does not know (in the NIB) the length of the various titles that the button will adopt, how can I guarantee that the button will, for each localization, be sized in such a way that it will fit the longest-length title in each localization?
What is the right way to accomplish this? Many thanks.

You don't need to change much. You just add the alignment constraints whatever you added. Now give it width and height constraints and make them greater than equals to the minimum value you wanted. See this.
I have added center horizontal and vertical for alignment. This you can change to any. Now the width is here greater than equals to 42.0 and the height is greater than equals to 21.0 . Now if you just change the text of the button then it will increase its height and width according to that. You just need to call this line just after changing the text.
[self.view layoutIfNeeded];
Now you may have question that you want to limit the width and then increase height. Here is the solution. You give it width less than equals to max width and it will not increase the width more than that. See here.
Here my max width is 200.0. But make sure that if you are adding those layout constraints in iOS and it is a UILabel then than Lines is 0 and if it is a UIButton then the Line Break is Word Wrap or Character Wrap, but in OS X increasing height of NSButton depends on your button type.

If you have given constraints to your button, just remove it's fixed width constraint, button will automatically fit as per text's width.

Related

Autoalign changes width of my button

I'm setting width for my button so that it's greater than text size - as on image below:
The width of it is 120px. My problem is that I have center vertical alignment for it so when I hit "update frames" then my button gets resized to it's minimum so that text fits the button as below:
I can't leave it that way because I've added frame programatically and it looks bad when it's so close to the text.
Is there any way to set some properties for xcode to stick to my width as oppose to auto-align it? I'd prefer to have it in xcode properties than in swift code.
You are not adding a constraint to the width. You need to add a constraint to it, not change de width at the Size Inspector.
Select the element, click at the Pin option at the bottom bar, and set the width of your element. Update Frames and voilĂ .
Remember: when working with Auto Layout you cannot use any manual configuration (size, position, etc.) for that element, always use constraints or stack views.
Apple documentation about Auto Layout

How to make an item full width in auto layout constraint?

I'm trying to make a button full width in ios 9 storyboards, and am trying to create a width auto constraint, but the width constraint only seems to take an exact static number. How do I make the item go full width.
Additionally if I wanted say an imageview to be full height and adjustable width to maintain it's aspect ratio - or a size to fit fill, how would I do that with the auto layout constraints in the storyboard.
Should I be doing this programmatically in the view controller?
For the button:
In storyboard drag the left and right sides of the button to make it full width. Make sure the button is selected and add a pin constraint to the left and right (if you've dragged it full width the pin constraint should be equal to -20)
For the imageView:
The full height constraint is done almost exactly the same, although this time you need to drag the top and bottom to the top and bottom edges of the view controller and pin the top and bottom to their respective Layout guide.
You can make the image full screen in the view controller (pin top, bottom, left & right) and then edit the aspect ratio of the image in the Attributes inspector when the imageView is selected:
Hope that helps!

IOS Auto layout not displaying the UILabel in multiple lines when content length is longer

I am trying to build a screen similar to twitter/instagram profile page where there is a header section and below that scrollable content.
I have a view controller which has a table view. I wanted to include a header view on top of the table cells. So i dragged and dropped a "View" just above my cell and it works fine. Now i have a label inside this header view and i want the label height and UI view height to adjust automaticlly based on the content length of the label. (If the content is a long string, the number of lines of label should automatically increase and the container view(headerview)'s height should increased based on it's content items property.
I tried to set auto layout on the table(top left right and bottom to the container view(headerview). But then the label is shown in single line, truncated at the very right corner. If i remove the bottom constraint from my constraints, the label appear in multiple lines, but the container view(headerview)'s height is not increased, thus producing an overlap of the label on top of the table below that.
I have the number of lines property set to 0 for the label.
Can anyone share some ideas how to do this ? I am using Xcode 7 and swift 2
There are several approaches for what you want to accomplish. If you do not care if the header scrolls with the cells, the easiest way is to do something like this:
----------------
Header
Label
TableView
----------------
You should pin your header Top,Right,Left.
You should pin your tableView Right,Bottom,Left.
You should pin Header.bottom = TableView.top.
You should pin your Label 4 sides to it's superview(Header).
Set Label numberOfLines = 0
If your header has a fixed width, the label will automatically inherit that width, and setting numberOfLines=0 (a.k.a unlimited number of lines) will make your label increase it's height. Because your label is pinned to it's superView by it's 4 sides (and the header has no fixed height), your header must increase it's height to match the label height.
Hope it helps!!

Xcode Autolayout - Setting UIButton Width equal to view width

I have a view with one UIButton with width equal to superview width. It renders correctly for iPhone 4s,5s but for 6+ it renders as shown below. I have tried adding constraints "trailing space to" "leading space to" to make it end to end but its not working and being new to layout I am not able to figure out where I am going wrong. Any suggestion is appreciated.
There are for things you have to work out with when you use autolayout...i.e.
x position of object
Y position of object
Height of Object
Width of object
here is the image for your button constraints and how it works...
At the first stage where i pinned leading space,trailing space and height i.e gives button height, width and x position....
But still i need Y position... so i gave Horizontal and vertical center constraints....
instead of this you can use top space constrain or only use vertical constraint instead of both Horizontal and vertical center constraints for Y position....
and the output screen is
In your "Add new constraints" popup do following:
Disable "Constrain to margins" checkbox
Add left and right constraints, set constants to 0.
Add height constraint.
Click "Add 3 constraints"
That's it!

Flexible height with dynamic layout in Interface Builder

I'm new to the Dynamic Layout concept introduced in Cocoa applications with Lion 10.7.
I've been playing with some examples and almost all works as I expect. But, there is one thing I've been unable to get: Flexible Height in some elements.
I have one NSTextField element where I can set properly the following constraints:
Leading to trailing -> To keep it "near" to left and right border resizing its width.
Vertical space -> To keep it "near" to the previous vertical element.
I also set:
Vertical space -> To keep it "near" to the bottom border.
Height >= x -> With the previous one, to make its height "flexible" and adjustable to the view's height when resized.
When I test it, it works in the "horizontal axis" (location and size) but it doesn't allow me to resize the window's height (it's fixed).
If I delete the "Vertical space" constraint that attaches the NSInputText to the view's bottom, I can resize the window but the NSInpuntText's height remains unchanged.
Another annoying thing is that the "default" height constraint for the NSInputText (that on "pink" color) can't be deleted or modified. Whenever I do that a new one is created.
Any ideas?
Thanks.
UPDATE
If I use a "Text View" (NSScrollView with a NSTextField inside) instead of a "plain" NSTextField I'm able to create the behavior I want with any problem.

Resources