3

I have four labels stacked one below the previous one but alighning its baseline with the top of its content view, not with a vertical spacing with each other.

I do it by code this way

[contentView addConstraint:[NSLayoutConstraint constraintWithItem:topFirstLabel_
                                                        attribute:NSLayoutAttributeBaseline
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:contentView
                                                        attribute:NSLayoutAttributeTop
                                                       multiplier:1.0f
                                                         constant:20.0f]];

[contentView addConstraint:[NSLayoutConstraint constraintWithItem:topSecondLabel_
                                                        attribute:NSLayoutAttributeBaseline
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:contentView
                                                        attribute:NSLayoutAttributeTop
                                                       multiplier:1.0f
                                                         constant:47.0f]];

[contentView addConstraint:[NSLayoutConstraint constraintWithItem:topThirdLabel_
                                                        attribute:NSLayoutAttributeBaseline
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:contentView
                                                        attribute:NSLayoutAttributeTop
                                                       multiplier:1.0f
                                                         constant:70.0f]];

[contentView addConstraint:[NSLayoutConstraint constraintWithItem:topFourthLabel_
                                                        attribute:NSLayoutAttributeBaseline
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:contentView
                                                        attribute:NSLayoutAttributeTop
                                                       multiplier:1.0f
                                                         constant:87.0f]];

Now, I want all labels to be aligned by the trailing space with its superview.

Can I do that with an unique VFL string? Something like this, although this example will crash the app:

NSDictionary *views = NSDictionaryOfVariableBindings(contentView, topFirstLabel_, topSecondLabel_, topThirdLabel_, topFourthLabel_);
NSDictionary *metrics = @{ @"bigMargin" : @12 };

[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[topFirstLabel_][topSecondLabel_][topThirdLabel_][topFourthLabel_]-bigMargin-|"
                                                                    options:NSLayoutFormatAlignAllTrailing
                                                                    metrics:metrics
                                                                      views:views]];
emenegro
  • 6,901
  • 10
  • 45
  • 68
  • I don't think you'll be able to do that -- You have to have all the labels in the format string to align them, but you can't set other spacings, since you've already done that with the other methods. Why not have the labels be spaced vertically with respect to each other (instead of wrt to the contentView), then you can use VFL to do that spacing as well as the align all trailing edges? – rdelmar Jul 10 '13 at 15:47
  • @rdelmar I can't do that, I have to align each cell a fixed space from its superview top edge to the baseline. Nothing to do there :P Thanks for your response. – emenegro Jul 10 '13 at 19:07

2 Answers2

2

This library might help you. It uses a vertical linear layout concept and you can add paddings as needed.

Raphael Oliveira
  • 7,751
  • 5
  • 47
  • 55
1

I don't think you can do this in a single call.

You can probably do something like this:

for ( NSString* viewName in views.allKeys )
{
    [contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: [NSString stringWithFormat: @"H:[%@]-bigMargin-|", viewName], options:NSLayoutFormatAlignAllTrailing metrics:metrics views:views];
}
TomSwift
  • 39,369
  • 12
  • 121
  • 149