MASExampleLabelView.m
3.18 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// 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) UIButton *bigButton;
@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.bigButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.bigButton setTitle:@"Big Button" forState:UIControlStateNormal];
[self.bigButton setContentEdgeInsets:UIEdgeInsetsMake(20, 5, 20, 5)];
self.bigButton.layer.borderColor = UIColor.blueColor.CGColor;
self.bigButton.layer.borderWidth = 1;
[self addSubview:self.bigButton];
self.shortLabel = UILabel.new;
self.shortLabel.numberOfLines = 3;
self.shortLabel.textColor = [UIColor purpleColor];
self.shortLabel.lineBreakMode = NSLineBreakByTruncatingTail;
self.shortLabel.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.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.bigButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).insets(kPadding);
make.right.equalTo(self).insets(kPadding);
make.width.equalTo(@100);
}];
[self.shortLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).insets(kPadding);
make.left.equalTo(self).insets(kPadding);
make.right.equalTo(self.bigButton.mas_left).offset(-5);
}];
[self.longLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.shortLabel.mas_bottom).offset(kPadding.bottom);
make.left.equalTo(self).insets(kPadding);
make.right.equalTo(self).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
self.shortLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.shortLabel.frame);
self.longLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.longLabel.frame);
// need to layoutSubviews again as frames need to recalculated with preferredLayoutWidth
[super layoutSubviews];
}
@end