ios - 调整单元格大小以适合标签,已设置图像大小并且标签有一些圆角

标签 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

提前感谢您的帮助。

最佳答案

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/

相关文章:

ios - 在单元格之间的 UICollectionView 中设置/覆盖填充

ios - 自动选择本地 CocoaPods 框架的正确路径

iphone - 如何在 iPhone 的 MKAnnotationView 中的标题下显示图像

ios - UIImageView 在动画时不剪裁到 UIView 边界

ios - 使用 SpriteKit 创建一条直线

ios - SKScene 中未创建 SKLabelNode – Swift

ios - 在 UISearchController 中搜索时无法在 tableView 中滚动

ios - Swift - IBACTION 如何执行 segue

ios - 在 Swift 中为 UIButton 设置自动布局约束

ios - 如何在滚动条上隐藏搜索栏? (没有 UITableViewController)