-->

Add Custom label in JSQViewMessageViewController i

2019-09-02 19:41发布

问题:

I want to add a custom label (with a time stamp) in each cell as well as an image (for warning message) in JSQMessageViewController. I am already using bottomlabel as well as toplabel. But I am unable to get the result I want. The image is a reference of what I would like it to look like

回答1:

UIImage *bubbleImage = [UIImage imageNamed:@"Commentbox_right.png"];
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(textFrame.origin.x-2, textFrame.origin.y-2, textFrame.size.width+4 , textFrame.size.height+7)];
        imgView.image= [bubbleImage stretchableImageWithLeftCapWidth:bubbleImage.size.width/2-5 topCapHeight:bubbleImage.size.height/2];
        [cell addSubview:imgView];
        [cell bringSubviewToFront:txtViewMessage];

        UILabel *lblTimeStamp = [[UILabel alloc]initWithFrame:CGRectMake(textFrame.origin.x+2, imgView.frame.size.height+imgView.frame.origin.y, 90, 10)];
        [lblTimeStamp setText:message.dateTime];//set time here
        [lblTimeStamp setFont:FONT_300_LIGHT(7)];
        [lblTimeStamp setTextColor:GET_COLOR_WITH_RGB(129,129,129, 1)];
        [lblTimeStamp setTextAlignment:NSTextAlignmentLeft];
        [lblTimeStamp setBackgroundColor:[UIColor  clearColor]];
        [cell addSubview:lblTimeStamp];


回答2:

Sorry i didn't get much time to look in to it .. but again you can add that image in same message bubble xib and add constraints according to your need. Try this xib



回答3:

How I add custom Label in JSQMessageviewcontroller is...
I declare Label text before ViewDidLoad.

 // Add Text Label
let myLabel: UILabel = {
    let lb = UILabel()
    lb.translatesAutoresizingMaskIntoConstraints = false
    lb.textAlignment = .center
    lb.numberOfLines = 1
    lb.textColor = UIColor.white
    lb.font=UIFont.systemFont(ofSize: 22)
    lb.backgroundColor = UIColor(red: 0.0/255.0, green:70.0/255.0, blue:110.0/255.0, alpha:1)
    lb.text = NSLocalizedString("No Notification", comment: "")

    return lb
}()


And add this code in viewDidiLoad or call anywhere you like.

 self.view.addSubview(myLabel)
 setUpMyLabel() 

This is how I add custom label in my app. Hope this will help you. :)