简体   繁体   中英

Align first line of multiple lines UILabel with UIImage using AutoLayout programmatically ( Objective-C )

I'm trying to align a multi-lines UILabel with a UIImage. The thing is : I want to align the first line of this label with the image, horizontally.

What I tried :

1 - align the image with the entire UILabel, so basically I still wanted to take the image up.

[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[image(width)]-8-[multiLinesLabel]|" options:NSLayoutFormatAlignAllCenterY metrics:metrics views:views]];

2 - So I tried this, but then the image was too high.

[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[image(width)]-8-[multiLinesLabel]|" options:NSLayoutFormatAlignAllTop metrics:metrics views:views]];

Has anybody got an idea to do that ?

Thanks in advance

EDIT : I finally did the trick like that :

[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[image(width)]-8-[multiLinesLabel]|" options:kNilOptions metrics:metrics views:views]];

[containerView addConstraint:[NSLayoutConstraint
                                      constraintWithItem:image
                                      attribute:NSLayoutAttributeTop
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:multiLinesLabel
                                      attribute:NSLayoutAttributeTop
                                      multiplier:1.0
                                      constant:3.0]];

I'm not really proud of me, it's not a clean way of solving the problem, but it works. If anyone thinks about an other solution...

You could add a single line dummy label with the same font and a single character of text. Make it hidden. Position it to align with your image, and then position the multi-line label's top edge to align with the dummy label.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM