标签显示我的自定义TableViewCell范围,并通过swift设置自动布局

时间:2017-07-15 06:42:32

标签: ios swift autolayout nslayoutconstraint ios-autolayout

我使用xib创建聊天气泡tableViewCell,但我使用自动布局来设置约束 它显示我的文字超出我输入的范围。
我也不想缩小文字。

![constrain ![out of range

我还添加了textLabel左边约束,它使我想要的不同类型 我不想像上一张图​​片一样显示聊天泡泡的空白区域 我该怎么办?

![add left constrain ![new constrain like this

更新:

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)
}
}

1 个答案:

答案 0 :(得分:0)

您创建标签宽度约束IBOutlet

Objectvice-C:

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

然后在CellForRowAtIndexPath:

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

Swift3:

  @IBOutlet var labelWidthConstraint: NSLayoutConstraint!

然后在cellForRowAt:

 cell.labelWidthConstraint.constant = UIScreen.main.bounds.size.width-80
 cell.msg.text = "text";
 cell.msg.autoresizingMask = [.flexibleWidth, .flexibleHeight]