set uibutton text alignment storyboard - ios

I know that in code I can do [myButton.titleLabel setTextAlignment: NSTextAlignmentRight]; but how would I do this in Storyboard in User Defined Runtime Attributes?

You can set the alignment of content (text or image) within your UIButton in the Attributes Inspector Panel.
The menu is "Control", you can set Horizontal & Vertical Alignment

How to make UIButton's text alignment center? Using IB
This post shows how to do it in storyboard.

You can also adjust the constraints of the content, title or image in the size inspector of the button:
In my case, I was only able to really centralize the character "+" and "-" after setting a value for the bottom of the title.

Related

Storyboard: centre text horizontally in UILabel

I know how to do it programmatically:
textLabel.textAlignment = .center
But isn't it possible to do that in storyboard? Unbelievable.
There is an appropriate property called "Content Mode: Center" in the attributes inspector, but seems it doesn't work.
As #Sweeper has said in the comments. The UILabel has an Alignment property which you can find under the Attributes Inspector tab, set the value to Center represented by horizontally centred dashes.

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

Centering a custom UIButton title with custom font

I'm using some custom fonts I added to my project for the titles of UIButton. I am not able to set my custom fonts in IB, and I've read in some posts that it is not possible yet in the last XCode version, is that right? So, currently, I'm setting the font for the titles programmatically. The point is that, as in IB the font is set to the default, when I set my custom font programmatically it is not centered, I guess because the font I can see in IB for the button has another height and width and this is the size considered to center the title. How could I center the title with my custom font?
Thanks!
Try this.
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
The problem came from the custom font, that it introduced some space below characters. This post solved my problem:
UIButton custom font vertical alignment

UIButton changing width when label changes

I'm writing a calculator app with a shift key. When the shift key is pressed, some of the UIButtons' labels should change. The problem is that when they do, the widths also change and, because of certain design constraints, other buttons will resize themselves too. How to I lock the width of a button (either pragmatically or in the storyboard builder) so that it will never change (regardless of the contents of the button)
Thanks for any help in advance.
Buttons, like labels have an intrinsic size that's set by the width of their text plus some padding. If you want them to have a fixed width, then in IB, just give each button an explicit width from the menu, Editor --> Pin --> Width.
Have you tried using [button sizeToFit]? Call it after you resize the subviews within and then you will likely want to apply some "padding" as this will size the view to exactly fit it's subviews.
UIView sizeToFit

Show background color in UILabel only as long as the text is

I've set the backgroundColor via the attributes tab for the view controller in my Storyboard. But I want to show the background color only in the length of the text which is filled into the label, not in the whole possible length.
I can not attach any image, so here is what I would like to have:
Try the following steps:
Select the UILabel in the Storyboard.
Open the "Size Inspector."
In the "Label" section, make sure "Explicit" is unchecked.
In the list of "Constraints," make sure that no constraint provides a value for the width property. Also, remove any constraints that pin the right edge of the UILabel to the UIImageView as this will implicitly give Auto Layout a fixed width for the label.
Add an "Equal Widths" constraint between the UILabel and the UIImageView. This can be achieved by control-dragging from the label to the image view in the Storyboard and selecting "Equal Widths" from the menu that appears.
Select either the label of the image view. Open the "Size Inspector" again double click on the box that corresponds to the constraint that you just created. In the "Relation" drop down menu, choose "Less Than or Equal".
This will cause the UILabel to dynamically change it's width depending on the width of the text drawn inside it. When you set the backgroundColor property, it will still fill the label by will only show behind the text. The Equal Widths constraint will make sure that the width of the label never exceeds the width of the image view.

Resources