Question

I've got a UIImageView and a UILabel, one sit next to another with a fixed spacing (let say 10pt). The UILabel's width may change dynamically. Is it possible to align them centre horizontally like below ?

|    @ Abc    |
|             |
|             |

|   @ Abcde   |
|             |
|             |
Was it helpful?

Solution

Put both of them in a view like this:

|<-- center view  -->|
|  ________________  |
| |       View     | |
| |_______   ______| |
| | Image | | Label| |
| |_______| |______| |
| |________________| |
|                    |

Then center the view horizontal. Within the view you need to define constraints for the imageview and the label so that these define the height and width of the view.

EDIT Here are two images that show it works:

Short text Longer text

OTHER TIPS

set UILabel's text alignment to center. then move it to the center of the view so that there's a guid line appear in the center of the UILabel. sorry, miss understood your mean, updating... select both label and image by holding command key then move them to align to the center guid line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top