iOS: Localised UIButton With Image and Title Label With Auto Layout - ios

I'm wonder is there a way to make a button that includes UIImage and title label under the image using auto layout. I made this using title insets left with negative value. But, right now I want to add localisation to my app and, of course, title labels of my buttons won't be a constant value. I see the only way: calculate every insets for every button for a particular text (for every localisation). This is quite annoying.
Is there a way to replace insets with auto layout without separation of UIButton?

Related

How to control layout of UIBarButtonItem controls on an iOS toolbar?

On my toolbar I am trying to properly lay out three buttons. One in the middle that should remain centered horizontally, and then one aligned left and one aligned right. But because the text for the left button is longer than either the middle button or the right one, the spacer items won't seem to produce the desired spacing effect. I've tried both fixed and flexible, but of course using the fixed type will not dynamically respond to different screen widths, and the the flexible width ones (UIBarButtonSystemItemFlexibleSpace) will not keep the middle button centered.
I've defined the layout both in Interface Builder and programmatically and I get the same effect, what you see here. For example an iPhone 6s looks like:
and an iPhone 11 Pro looks like:
As you can see the "Mid" button is not centered. By using UIBarButtonSystemItemFixedSpace instead of UIBarButtonSystemItemFlexibleSpace I could define the width of the spacers exactly, but in order to calculate it properly I would need to know the width of each UIBarButtonItem and also the right delegate method to do this in. Is this even possible, or must I do a custom view (or something else) to make this UIToolbar look like I want?
Try adding leading spaces to the Shorter title OR using the following items:
Longer-Text Button, Flexible Space, Mid Button, Flexible Space, Fixed Space, Shorter Button
The fixed space would be a hard coded value to match the width delta between the two buttons.

Label not displaying properly

I am beginner in iOS app development, I am working on a project that was done by someone else. So my problem is the label is not fully showing its contents:
check the second column under 'customer name'.
I did some basic alterations to the label but it makes no change at all. This is a collection view, there is another view inside the collection view cell which holds the title label and description label.
I searched everywhere but didn't get any proper answer please help.
Label in iOS does not change its size to suit the contents, you need to adjust it in your code. Your options are either decrease the font size to suit the description label size, or adjust the description label's height/width to make room for current content. I'd do a combination of both (slightly reducing the font size of the label's text, and at the same time making all the cells a bit wider and taller).
Oh, there is also a way to automatically shrink the font size if the contents doesn't fit the space. So, you'd need to check the option for your description label. Here's how you might do it: how to make UILabel autosize text in storyboard (or interface builder), NOT programmatically

Stop UITextField from expanding horizontally

I have a UITextField positioned in a view next to a button. It has a trailing constraint of 8 to the button (which has a trailing constraint of 8 to the superview) and when I type long text in it, it simply scrolls along, which is I want. However, in order to retain the text typed in the field if the view is switched to another one (it's in a tab controller), I save the text in a holder variable and when it switches back to that view, I set the text in the field to the saved text.
The problem is that this causes the field to expand horizontally if the text is long enough, sometimes pushing the button off-screen, even with the trailing 8 constraint. I have tried to save the original frame of the field in a holder variable, and then after setting the text, set the frame to the saved original frame like so:
fieldFrame = messageField.frame
println(messageField.frame.width)
messageField.text = holderMessage
println(messageField.frame.width)
messageField.frame = fieldFrame
However, the field still expands, and it printed out 502.0 twice. My current thought is that the frame hasn't registered the change in width after the setting of the text in time for the println, but I'm not sure if this is correct.
Also, I've read some similar questions that suggested using a width constraint. If I use a less than or equal to width constraint on the field, will it still expand if on a device that's thinner? That is to say, since I'm currently using an any width and any height storyboard, it's wider than, say, an iPhone 6. So if I set a less than or equal to width constraint on the current width of the field, it seems possible that the field can still expand on a smaller device and not break that constraint.
Is there a better way to do such a width constraint? If not, how else can I keep the field from expanding and pushing the button offscreen?
Here's the problem. The text field has a tendency to size itself horizontally to its contents. The button has a tendency to size itself horizontally to its contents. Thus you have an ambiguity: those tendencies have the same priority, so the runtime doesn't know which one to collapse as the text field's text gets too long.
The solution is to adjust those priorities. You must lower the horizontal compression and hugging priorities for the text field - one point lower should be sufficient. Now the button will have a stronger tendency to match its own size to its contents, and the text field will clip its contents.
You can also lower the Content Compression Resistance programmatically (this also works if you are using a UIViewRepresentable in SwiftUi):
uiTextField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
For more info on this topic please refer to:
https://medium.com/#dineshk1389/content-hugging-and-compression-resistance-in-ios-35a0e8f19118
Selecting the Text view, then within Size inspector:
1) Set "Layout Margins" to "Fixed".
2) Under "Content Compression Resistance Priority", set the "Horizontal" to be "Low (250)".

Give dynamic title to button and resize it without setting height constraint

I added a button to my view in viewcontroller and I gave it only positioning constraints (which are top space and leading space). In my button I added a set, an image and a title which is three four words. I have other views in the same horizontal row. I set their title and edge insets programmatically so both are aligned to center and title is just below the button.
How can I set its title dynamically so it will show full title? I have already used sizeToFit function that doesn't help.
Determine the size of your button title label using boundingRectWithSize:options:context:, then set the UIButton.frame accordingly.
Apple docs - boundingRectWithSize:options:context:
Edit:
Autolayout resize button based on text and have textfield fill available space

iOS: UILabel & UIButton in a table cell

I am trying to display a Label and a button side-by-side in a UITableViewCell. The button sits at the right end & the Label sits at the left end. The label can host a long text and should get truncated if the text would flow out of bounds (I do not want to reduce the font size, etc.). Needless to say, I want this to work for both the orientations.
If I use UITableViewCellStyleDefault (without adding a button) & set a long text for the default label, I observe that the label auto-resizes perfectly when the orientation changes. Most probably because its autoresizingMask is set to UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin. Now, the way I see it, I could insert my button, and make the label truncate properly, if I could configure the value of the right margin that is being used by default (to accommodate the button). I essentially want it to auto-resize within the bounds that I specify. Is there a way this can be accomplished?
I would not prefer listening to each orientation change and setting the bounds of the label's frame. Any other feasible solution?
I finally went with subclassing UITableViewCell & overriding layoutSubViews. Thanks Mark!

Resources