0

我正在开发一个聊天应用程序,我正在使用alex barinov 的聊天气泡示例。我能够发送/接收消息。我想在每个聊天气泡上添加一些标签和视图,例如时间、日期、交付的图像等。

我尝试UIBubbleTableViewCell.m在方法 named 的类中的聊天气泡的 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];
}

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

4

1 回答 1

0

嘿,我认为您必须创建一个视图并通过它。

在 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 。

于 2016-10-19T13:24:59.973 回答