调整单元格大小以适合标签,设置图像大小和标签有一些圆角

时间:2016-09-08 16:56:06

标签: ios swift

我正在尝试让单元格调整其内部的文本/图像。但是标签也有一些圆角,具体取决于它们的位置。我尝试使用像:

这样的代码片段
    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
}

这就是我想要实现的目标

This is what im trying to achieve

感谢您提前提供任何帮助。

1 个答案:

答案 0 :(得分:0)

  

但是我不能让这两个人一起工作。

有两个问题:

  • 时序。调整视图大小时,不会调整其蒙版大小。因此,如果您在知道视图的最终大小之前附加了掩码,则会得到错误的答案。

  • Resuse。不要忘记重复使用一个单元格。因此,在cellForRow中,您总是需要删除现有的掩码(如果有的话),因为您正在做另一个掩码以适应新调整大小的单元格。