// // MASExampleLabelView.m // Masonry iOS Examples // // Created by Jonas Budelmann on 24/10/13. // Copyright (c) 2013 Jonas Budelmann. All rights reserved. // #import "MASExampleLabelView.h" static UIEdgeInsets const kPadding = {10, 10, 10, 10}; @interface MASExampleLabelView () @property (nonatomic, strong) UILabel *shortLabel; @property (nonatomic, strong) UILabel *longLabel; @end @implementation MASExampleLabelView - (id)init { self = [super init]; if (!self) return nil; // text courtesy of http://baconipsum.com/ self.shortLabel = UILabel.new; self.shortLabel.numberOfLines = 1; self.shortLabel.textColor = [UIColor purpleColor]; self.shortLabel.lineBreakMode = NSLineBreakByTruncatingTail; self.shortLabel.text = @"Bacon"; [self addSubview:self.shortLabel]; self.longLabel = UILabel.new; self.longLabel.numberOfLines = 8; self.longLabel.textColor = [UIColor darkGrayColor]; self.longLabel.lineBreakMode = NSLineBreakByTruncatingTail; self.longLabel.text = @"Bacon ipsum dolor sit amet spare ribs fatback kielbasa salami, tri-tip jowl pastrami flank short loin rump sirloin. Tenderloin frankfurter chicken biltong rump chuck filet mignon pork t-bone flank ham hock."; [self addSubview:self.longLabel]; [self.longLabel makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.left).insets(kPadding); make.top.equalTo(self.top).insets(kPadding); }]; [self.shortLabel makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.longLabel.centerY); make.right.equalTo(self.right).insets(kPadding); }]; return self; } - (void)layoutSubviews { [super layoutSubviews]; // for multiline UILabel's you need set the preferredMaxLayoutWidth // you need to do this after [super layoutSubviews] as the frames will have a value from Auto Layout at this point // stay tuned for new easier way todo this coming soon to Masonry CGFloat width = CGRectGetMinX(self.shortLabel.frame) - kPadding.left; width -= CGRectGetMinX(self.longLabel.frame); self.longLabel.preferredMaxLayoutWidth = width; // need to layoutSubviews again as frames need to recalculated with preferredLayoutWidth [super layoutSubviews]; } @end