2016-10-19 163 views
0

聊天气泡(小区)我上的聊天应用我在哪里使用亚历克斯巴里诺夫的聊天气泡示例工作。我能够发送/接收消息。我想补充的,如时间,日期,交付图像每个聊天泡泡一些标签和视图等添加子视图在IOS

我试图在聊天气泡的内容查看在名为-(void)setupInternalData方法UIBubbleTableViewCell.m类添加一些标签,但标签(日期标签)重复每个泡泡,它会覆盖以前的标签,并且它包含两个标签。

这里是从我下载了该项目的地址 - 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]; 
} 

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

+0

尝试初始化并从初始化程序添加子视图而不是单独的方法,并从其他地方调用它。 –

+0

@ Rohan Bhale - 请你详细说明一下,因为我是iOS新手。在哪里准确地添加上面的代码?请。 –

+0

https://github.com/jessesquires/JSQMessagesViewController –

回答

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]; 
} 
在这一点,你必须创建一个视图

,在这个视图中添加uilable,UIImage的要与自定义视图替换的UILabel *标签视图。

+0

感谢您的建议。 –

相关问题