1010
1111@interface RJTableViewCell ()
1212
13- // We'll set this to YES once this instance of the cell has setup its constraints, so that any additional calls to updateConstraints are no-ops
1413@property (nonatomic , assign ) BOOL hasSetupConstraints;
1514
1615@end
@@ -31,33 +30,17 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
3130 [self .titleLabel setFont: [UIFont fontWithName: @" Helvetica-Bold" size: 21 ]];
3231 [self .titleLabel setTextColor: [UIColor blackColor ]];
3332 [self .titleLabel setBackgroundColor: [UIColor clearColor ]];
34- // [self.titleLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
35- // [self.titleLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
36-
33+
3734 self.bodyLabel = [[UILabel alloc ] initWithFrame: CGRectZero];
3835 [self .bodyLabel setTranslatesAutoresizingMaskIntoConstraints: NO ];
36+ [self .bodyLabel setContentCompressionResistancePriority: UILayoutPriorityRequired forAxis: UILayoutConstraintAxisVertical];
3937 [self .bodyLabel setLineBreakMode: NSLineBreakByTruncatingTail];
40-
41- // You don't want the below line; let the font size for the multi-line body label remain fixed.
42- // [self.titleLabel setAdjustsFontSizeToFitWidth:YES];
43-
4438 [self .bodyLabel setNumberOfLines: 0 ];
4539 [self .bodyLabel setTextAlignment: NSTextAlignmentLeft];
4640 [self .bodyLabel setFont: [UIFont fontWithName: @" Helvetica" size: 14 ]];
4741 [self .bodyLabel setTextColor: [UIColor darkGrayColor ]];
4842 [self .bodyLabel setBackgroundColor: [UIColor clearColor ]];
49-
50- // If you don't do this, the auto layout solver engine may calculate a ever-so-slightly smaller height than actually required
51- // (probably due to rounding errors internally) which will cause the bodyLabel to truncate the last line. This will force
52- // the bodyLabel to always get the size it needs for all the lines. (The titleLabel height will be reduced ever so slightly
53- // in this case if needed to satisfy this constraint, but in this case it shouldn't be noticeable.)
54- // Another way to solve this is to return a slightly larger cell height (add 1 point) than the auto layout calculations return.
55- // Or a third way is to build in a few points of padding (perhaps between the labels, or above or below them) which has a priority of
56- // UILayoutPriorityDefaultHigh - 1, so that it will be the first thing to be reduced if the cell height is a tiny bit too small -
57- // it will act as a buffer to prevent the labels from getting smaller than the size they really want.
58- [self .bodyLabel setContentCompressionResistancePriority: UILayoutPriorityRequired forAxis: UILayoutConstraintAxisVertical];
5943
60- // Add your subviews right away during init - no reason to wait until later
6144 [self .contentView addSubview: self .titleLabel];
6245 [self .contentView addSubview: self .bodyLabel];
6346 }
@@ -68,8 +51,6 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
6851- (void )updateConstraints {
6952 [super updateConstraints ];
7053
71- // Modified to a single-line return. No biggie.
72- //
7354 if (self.hasSetupConstraints ) return ;
7455
7556 [self .contentView addConstraint: [NSLayoutConstraint
@@ -99,31 +80,7 @@ - (void)updateConstraints {
9980 multiplier: 1 .0f
10081 constant: -kLabelHorizontalInsets ]];
10182
102- // You don't need the below constraint - the auto-generated content compression resistance constraints on UILabel are sufficient!
103- // [self.contentView addConstraint:[NSLayoutConstraint
104- // constraintWithItem:self.titleLabel
105- // attribute:NSLayoutAttributeHeight
106- // relatedBy:NSLayoutRelationGreaterThanOrEqual
107- // toItem:nil
108- // attribute:NSLayoutAttributeNotAnAttribute
109- // multiplier:1.0f
110- // constant:22.0f]];
111-
112- // "You don't need the below constraint - the auto-generated content compression resistance constraints on UILabel are sufficient!"
113- //
114- // OK, this is weird. Without NSLayoutAttributeHeight-NSLayoutRelationGreaterThanOrEqual self.titleLabel collapses?
115- // Adding the NSLayoutAttributeHeight-NSLayoutRelationGreaterThanOrEqual resolves issue. Perhaps there is another way...
116- // ...
117- [self .contentView addConstraint: [NSLayoutConstraint
118- constraintWithItem: self .titleLabel
119- attribute: NSLayoutAttributeHeight
120- relatedBy: NSLayoutRelationGreaterThanOrEqual
121- toItem: nil
122- attribute: NSLayoutAttributeNotAnAttribute
123- multiplier: 1 .0f
124- constant: 22 .0f ]];
125-
126- // /////////////////////////////////////////////////////////////////////////////////////////
83+ // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
12784
12885 [self .contentView addConstraint: [NSLayoutConstraint
12986 constraintWithItem: self .bodyLabel
@@ -161,16 +118,6 @@ - (void)updateConstraints {
161118 multiplier: 1 .0f
162119 constant: -(kLabelHorizontalInsets / 2 )]];
163120
164- // You don't need the below constraint - the auto-generated content compression resistance constraints on UILabel are sufficient!
165- // [self.contentView addConstraint:[NSLayoutConstraint
166- // constraintWithItem:self.bodyLabel
167- // attribute:NSLayoutAttributeHeight
168- // relatedBy:NSLayoutRelationGreaterThanOrEqual
169- // toItem:nil
170- // attribute:NSLayoutAttributeNotAnAttribute
171- // multiplier:1.0f
172- // constant:22.0f]];
173-
174121 self.hasSetupConstraints = YES ;
175122
176123}
0 commit comments