MASExampleLabelView.m
2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// 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.firstBaseline.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