在 iOS 中将子视图添加到聊天气泡(单元格)

adding subview to chat bubble (cell) in iOS

我正在开发一个聊天应用程序,我在其中使用了alex barinov 的 聊天气泡示例。我可以 send/receive 消息。我想在每个聊天气泡上添加一些标签和视图,例如时间、日期、传送的图像等。

我尝试在 UIBubbleTableViewCell.m class 中的聊天气泡的 contentView 上添加一些标签,方法名为 -(void)setupInternalData,但每个气泡都会重复标签(日期标签),并且它会覆盖之前的标签,并且包含这两个标签。

这是我下载项目的地址-https://github.com/AlexBarinov/UIBubbleTableView

下面是我为聊天气泡视图添加标签的代码 -

-(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];
}

任何人都可以告诉我我在这里做错了什么,我应该做什么。任何帮助表示赞赏。等待您宝贵的答案。

嘿,我想你必须创建一个视图并传递它。

在 NSBubbleData 文件的

中创建一个视图
- (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];
}

在这里你必须创建一个视图,在这个视图中添加你想要用自定义视图替换 UILabel *label 的 uilable,uiimage。