denikov
denikov

Reputation: 869

drawRect UIView resizable height inside UITableViewCell

I started playing around with drawRect method for a UIView trying to make a message bubble. This is what I got so far (it's not pretty but it's a start; open to suggestions to make it look better):

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setFrame:CGRectMake(60, 5, 250, 50)];
        self.contentMode = UIViewContentModeRedraw;
    }
    return self;
}

- (void)drawRect:(CGRect)rect{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1.0);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 0, 11);
    CGContextAddCurveToPoint(context,5,12.5,10,15,10,10);
    CGContextAddCurveToPoint(context, 10, 1, 20, 1, 20, 1);
    CGContextAddCurveToPoint(context, 20, 1, 50, 1, 240, 1);
    CGContextAddCurveToPoint(context, 240, 1, 250, 1, 249, 15);
    CGContextAddCurveToPoint(context, 249, 30, 249, 40, 249, 40);
    CGContextAddCurveToPoint(context, 249, 45, 245, 49, 240, 49);
    CGContextAddCurveToPoint(context, 240, 49, 200, 49, 20, 49);
    CGContextAddCurveToPoint(context, 15, 49, 10, 45, 10, 40);
    CGContextAddCurveToPoint(context, 10, 40, 10, 30, 10, 20);
    CGContextAddCurveToPoint(context, 10, 20, 3, 20, 0, 10);
    CGContextStrokePath(context);
}

In my custom cell I have a UILabel and I'm setting the custom view like this:

Bubble *bubble = [[Bubble alloc]init];
_bubble = bubble;
[self addSubview:_bubble];

UILabel *message = [[UILabel alloc]init];
message.backgroundColor = [UIColor yellowColor];
message.layer.borderWidth = 1;
[message setFont:[UIFont systemFontOfSize:12]];
message.numberOfLines = 0;
message.preferredMaxLayoutWidth = 200;
_message = message;
[bubble addSubview:_message];

I resize the UILabel using this method:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //...
    CGFloat height = [self heightForText:[[individualMessages 
        objectAtIndex:indexPath.row] valueForKey:@"theMessage"]];
    cell.message.frame = CGRectMake(20, 5, 220, height);
    //...
    return cell;
}

- (CGFloat)heightForText:(NSString*)text{
    ChatCell *cell = [[ChatCell alloc]init];
    cell.message.text = text;
    cell.message.font = [UIFont systemFontOfSize:12];
    [cell.message sizeToFit];
    return cell.message.frame.size.height;
}

My question is how can I pass the height of the "message" UILabel to the subclassed UIView and draw the bubble? Right now I'm just including the subclass in the custom cell but of course that's not resizing. Is there a way to pass the height to the UIView and include it later to the cell?

Upvotes: 0

Views: 865

Answers (1)

user3386109
user3386109

Reputation: 34839

self.bounds.size should give you the size of the view in the drawRect method.

You should also set the contentMode property of the view to UIViewContentModeRedraw so that drawRect is called when the view is resized. Otherwise, your existing bubble may just get stretched when the view resizes.

Upvotes: 1

Related Questions