Linh
Linh

Reputation: 60913

IOS - UIButton text align left Equal the text of another UIButton contains image

I am new in IOS. I want to know how to make the text in UIButton align left equal the text of another UIButton that contains an image.
Which flexible way to achieve it? (I mean flexible way here is when I change the size of the image the text align still correct)
Can I do it by config UI only or I need to code?
Here is the image description

Here is the image description

Upvotes: 1

Views: 137

Answers (2)

Anshad Rasheed
Anshad Rasheed

Reputation: 2566

I think it may help you

CGFloat Imagewidth = 10; // width of the image you need to set 
firstButtonBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, Imagewidth);
seconbutton.titleEdgeInsets = UIEdgeInsetsMake(0, Imagewidth, 0, 0);

Upvotes: 1

TheEye
TheEye

Reputation: 9346

As both the image and the text are part of the UIButton and hidden in interface builder, this is a bit tricky to achieve.

Some ways to do it:

1) Add an empty image with the same width to the second button, and keep those two images the same width.

2) Play with the title insets on the second button to move the text to the right position. The title insets will be related to the image width.

3) Don't make the text part of the button, but set it as UILabel on top of it.

Disadvantage: You won't get the button changes in your text (different color on press for example).

Advantage: Constraints can be used easily, and you get all benefits of UILabels like multiline and so on.

Upvotes: 2

Related Questions