I have a subclass of a UITableViewCell to show a custom cell. This class is integrated with Storyboard and therefore the frames of the properties have already been set in IB.
My aim is to dynamically set a label's width according to the text it contains and redraw the labels with setNeedsLayout.
Part of the .m file for the custom cell:
static CGFloat const kHorizontalEdgeOffset = 74.0;
static CGFloat const kVerticalEdgeOffset = 10.0;
static CGFloat const kNameLabelHeight = 22.0;
- (void)setFriend:(Person *)friend
{
if (_friend != freeDomeFriend) {
_friend = friend;
self.friend = friend;
}
// Set the text for the first name and save the size to fit the text.
self.firstNameLabel.text = friend.firstName;
self.firstNameLabelSize = [self.firstNameLabel.text sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:17.0]];
// Set the text for the last name and save the size to fit the text.
self.lastNameLabel.text = friend.lastName;
self.lastNameLabelSize = [self.lastNameLabel.text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:17.0]];
// Ask the view to layout the view with the changes.
[self setNeedsLayout];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)layoutSubviews
{
[super layoutSubviews];
// Set the new frames according to the caluclated size for the labels.
self.firstNameLabel.frame = CGRectMake(kHorizontalEdgeOffset, kVerticalEdgeOffset, kNameLabelHeight, self.firstNameLabelSize.width);
self.lastNameLabel.frame = CGRectMake(kHorizontalEdgeOffset + self.firstNameLabelSize.width + 2.0,
kVerticalEdgeOffset, kNameLabelHeight, self.lastNameLabelSize.width);
NSLog(@"FN: %@ \n LN: %@ \n ------------------", self.firstNameLabel, self.lastNameLabel);
}
My NSLog is giving me the correct output:
2012-05-12 23:56:36.741 MyProject[11421:16403] FN: <UILabel: 0xa888eb0; frame = (74 10; 22 57); text = 'Callum'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa880fa0>>
LN: <UILabel: 0xa8890c0; frame = (133 10; 22 45); text = 'Webb'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa889130>>
------------------
2012-05-12 23:56:36.742 MyProject[11421:16403] FN: <UILabel: 0xa88abf0; frame = (74 10; 22 48); text = 'Hervé'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa88ac60>>
LN: <UILabel: 0xa88ad00; frame = (124 10; 22 86); text = 'Fahendrich'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa88ad70>>
------------------
2012-05-12 23:56:36.743 MyProject[11421:16403] FN: <UILabel: 0xa88b4a0; frame = (74 10; 22 41); text = 'John'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa88b510>>
LN: <UILabel: 0xa88b5b0; frame = (117 10; 22 50); text = 'Clema'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa88b620>>
------------------
2012-05-12 23:56:36.743 MyProject[11421:16403] FN: <UILabel: 0xa88bf30; frame = (74 10; 22 40); text = 'Luke'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa88bfa0>>
LN: <UILabel: 0xa88c040; frame = (116 10; 22 74); text = 'McManus'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa88c0b0>>
------------------
However this is what I am getting visually (I can't upload a picture so I'll try my best to describe it): The labels have a very small size (width and height) and the origins seem to be random. Sometimes the lastName will be at the bottom of the cell and the first name right in the middle.
What am I doing wrong? I've been on this for hours.
Thanks of the help.