0

I use xib to creat a chat bubble tableViewCell, but I use auto layout to set the constrains.
It show my text out of range which I input.
And I also don't want to shrink the text.

![constrain ![out of range

And I also add textLabel left constrain, It makes different type which I want.
I don't want to show the empty region of chat bubble like last picture.
What should I do about this situation?

![add left constrain ![new constrain like this

Update:

class ChatMyTextTableViewCell: UITableViewCell {

@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var myTextLabel: PaddingLabel!
@IBOutlet weak var myDateLabel: UILabel!
@IBOutlet weak var labelWidthConstraint: NSLayoutConstraint!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    self.backgroundColor = defaultBackgroundColor

    myImageView.layer.masksToBounds = true 
    myImageView.layer.cornerRadius = defaultIconRadius

    myTextLabel.backgroundColor = defaultChatGreenBubbleColor
    myTextLabel.layer.masksToBounds = true
    myTextLabel.layer.cornerRadius = defaultButtonRadius

    myDateLabel.textColor = defaultChatTimeColor
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

func loadMessage(_ message:Message) {

    labelWidthConstraint.constant = UIScreen.main.bounds.size.width - 120
    myTextLabel.text = message.content
    myTextLabel.autoresizingMask = [.flexibleWidth,.flexibleHeight]

    myDateLabel.text = convertToChatDate(date: message.datetime)
}
}
tolerate_Me_Thx
  • 387
  • 2
  • 7
  • 21

1 Answers1

0

you create the label width Constraint IBOutlet

Objectvice-C:

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *msgWidthConst;

then in CellForRowAtIndexPath:

 cell.msgWidthConst.constant=[UIScreen mainScreen].bounds.size.width-80;
 cell.msg.text="text";
 cell.msg.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

Swift3:

  @IBOutlet var labelWidthConstraint: NSLayoutConstraint!

then in cellForRowAt:

 cell.labelWidthConstraint.constant = UIScreen.main.bounds.size.width-80
 cell.msg.text = "text";
 cell.msg.autoresizingMask = [.flexibleWidth, .flexibleHeight]
Vikash Kumar
  • 642
  • 1
  • 11
  • 25