标签显示超出我的自定义 TableViewCell 范围并通过 swift 设置自动布局
Label show out of my range of Customized TableViewCell and set auto-layout by swift
我使用 xib 创建聊天气泡 tableViewCell,但我使用自动布局来设置约束。
它显示我的文本超出了我输入的范围。
而且我也不想缩小文字。
我还添加了 textLabel 左约束,它使我想要的不同类型。
我不想像上图一样显示聊天气泡的空白区域。
这种情况怎么办?
更新:
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)
}
}
你创建标签宽度约束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;
斯威夫特3:
@IBOutlet var labelWidthConstraint: NSLayoutConstraint!
然后在 cellForRowAt:
cell.labelWidthConstraint.constant = UIScreen.main.bounds.size.width-80
cell.msg.text = "text";
cell.msg.autoresizingMask = [.flexibleWidth, .flexibleHeight]
我使用 xib 创建聊天气泡 tableViewCell,但我使用自动布局来设置约束。
它显示我的文本超出了我输入的范围。
而且我也不想缩小文字。
我还添加了 textLabel 左约束,它使我想要的不同类型。
我不想像上图一样显示聊天气泡的空白区域。
这种情况怎么办?
更新:
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)
}
}
你创建标签宽度约束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;
斯威夫特3:
@IBOutlet var labelWidthConstraint: NSLayoutConstraint!
然后在 cellForRowAt:
cell.labelWidthConstraint.constant = UIScreen.main.bounds.size.width-80
cell.msg.text = "text";
cell.msg.autoresizingMask = [.flexibleWidth, .flexibleHeight]