我正在尝试让一个单元格根据其中的文本/或图像调整大小。但标签也有一些圆角,具体取决于它们的位置。我试过使用像这样的片段:
let path = UIBezierPath(roundedRect:cell.receivedText!.bounds, byRoundingCorners:[.TopRight, .BottomLeft, .BottomRight], cornerRadii: CGSizeMake(30, 30))
let maskLayer = CAShapeLayer()
maskLayer.path = path.CGPath
cell.receivedText!.layer.mask = maskLayer
和
tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension
但我无法让两者协同工作。
这是我当前的代码减去不起作用的片段:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
createConversation()
let cell: ConversationCell = tableView.dequeueReusableCellWithIdentifier("ConversationCell") as! ConversationCell
if conversation.conversationArray[indexPath.row].isAReceivedMessage == true {
cell.receivedText!.text = conversation.conversationArray[indexPath.row].text
cell.receivedText!.textAlignment = .Left
cell.receivedText!.sizeToFit()
cell.receivedProfileImage!.image = self.conversationProfileImage!.image
cell.receivedText!.backgroundColor = UIColor(red: 44/255, green: 99/255, blue: 105/255, alpha: 5/100)
} else {
cell.sentText!.text = conversation.conversationArray[indexPath.row].text
cell.sentText!.textAlignment = .Right
cell.sentText!.backgroundColor = UIColor(red: 204/255, green: 115/255, blue: 115/255, alpha: 1)
cell.sentText!.sizeToFit()
}
return cell
}
这就是我要实现的目标
提前感谢您的帮助。
最佳答案
but i cant get the two to work in conjunction.
有两个问题:
时机。调整 View 大小时,不会调整其 mask 的大小。因此,如果在知道 View 的最终大小之前附加 mask ,就会得到错误的答案。
重新使用。不要忘记细胞是重复使用的。因此,在
cellForRow
中,您总是需要删除现有的 mask (如果有的话),因为您正在制作另一个 mask 以适应新调整大小的单元格。
关于ios - 调整单元格大小以适合标签,已设置图像大小并且标签有一些圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39396501/