Search code examples
iosobjective-cuitableviewaddsubviewuibubbletableview

adding subview to chat bubble (cell) in iOS


I am working on a chat application where i am using alex barinov's chat bubble example. I am able to send/receive messages. I want to add some labels and views on each chat bubble like time, date, delivered image, etc.

I tried to add some labels over the chat bubble's contentView in UIBubbleTableViewCell.m class in method named -(void)setupInternalData, but the label (date label) gets repeated for every bubble , and it overwrites the previous label, and it contains both the labels.

Here is the address from where i downloaded the project -https://github.com/AlexBarinov/UIBubbleTableView

Below is my code for adding labels to chat bubble view -

-(void)setupInternalData{

_fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.origin.x, self.frame.size.height-28, 100, 14)];
    _fromLabel.numberOfLines = 1;
    _fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
    _fromLabel.clipsToBounds = YES;
    _fromLabel.layer.cornerRadius=5;
    [_fromLabel setFont:[UIFont systemFontOfSize:5]];
    [self.customView addSubview:_fromLabel];



    //display msg sent time
    NSDate *today = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    NSString *currentTime = [dateFormatter stringFromDate:today];
    _lblMsgTime.text=currentTime;


    _lblMsgTime.frame=CGRectMake(12,0, 85, 14);
    _lblMsgTime.text=[NSString stringWithFormat:@"%@",currentTime];
    _lblMsgTime.textAlignment = NSTextAlignmentRight;
    _lblMsgTime.textColor = [UIColor blackColor];

    [_fromLabel addSubview:_lblMsgTime];
    [_lblMsgTime setAdjustsFontSizeToFitWidth:YES];

    //date formater
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"d.M.yyyy";
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    NSString *string = [formatter stringFromDate:date];
    _lblMsgdate.text=string;


    _lblMsgdate.frame=CGRectMake(60 ,0, 85, 14);
    _lblMsgdate.text=[NSString stringWithFormat:@"%@",string];
    _lblMsgdate.textAlignment = NSTextAlignmentRight;
    _lblMsgdate.textColor = [UIColor blackColor];

    [_fromLabel addSubview:_lblMsgdate];
    [_lblMsgdate setAdjustsFontSizeToFitWidth:YES];


    UIImageView *backgroundView =   [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic__typ__txt.png"]];
    backgroundView.frame=CGRectMake(self.frame.origin.x+1, self.frame.size.height-24, 10, 10);
    [self.customView addSubview:backgroundView];
    [self.customView bringSubviewToFront:_fromLabel];
    [self.customView bringSubviewToFront:backgroundView];
    [self.contentView addSubview:self.customView];
}

Can anyone please tell me what wrong am i doing here, and what should i be doing. Any help is appreciated. Waiting for your valuable answers.


Solution

  • hey i think you have to create a view and pass it.

    create a view in NSBubbleData file's

    - (id)initWithText:(NSString *)text date:(NSDate *)date type:(NSBubbleType)type
    {
        UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
        CGSize size = [(text ? text : @"") sizeWithFont:font constrainedToSize:CGSizeMake(220, 9999) lineBreakMode:NSLineBreakByWordWrapping];
    
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
        label.numberOfLines = 0;
        label.lineBreakMode = NSLineBreakByWordWrapping;
        label.text = (text ? text : @"");
        label.font = font;
        label.backgroundColor = [UIColor clearColor];
    
    #if !__has_feature(objc_arc)
        [label autorelease];
    #endif
    
        UIEdgeInsets insets = (type == BubbleTypeMine ? textInsetsMine : textInsetsSomeone);
        return [self initWithView:label date:date type:type insets:insets];
    }
    

    in this you have to create a view, in this view add uilable,uiimage which you want replace UILabel *label with custom view.