Resizing cells to fit labels, has set image size and label has some rounded corners

i am trying to get a cell to resize to the text/or image inside it. But also the labels have some rounded corners depending on their position. I have tried using snippets like:

    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

and

  • How do I modify a GPUImageGaussianSelectiveBlurFilter to operate over a rectangle instead of a circle?
  • How can I get the height of a specific row in my UITableView
  • Add a ScrollView to existing View
  • Swift. SIGABRT: cast value of type '__NSCFDictionary' to 'NSMutableArray'
  • NSTableView: detecting a mouse click together with the row and column
  • Javascript date is invalid on iOS
  •     tableView.estimatedRowHeight = 44
        tableView.rowHeight = UITableViewAutomaticDimension
    

    but i cant get the two to work in conjunction.

    Here is my current code minus snippets that didnt work:

    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

    This is what im trying to achieve

    Thanks for any help in advance.

    Solutions Collect From Internet About “Resizing cells to fit labels, has set image size and label has some rounded corners”

    but i cant get the two to work in conjunction.

    There are two problems:

    • Timing. When a view is resized, its mask is not resized. Thus, if you attach the mask before you know the final size of the view, you will get the wrong answer.

    • Resuse. Don’t forget that a cell is reused. Thus, in cellForRow, you always need to remove the existing mask if there is one, because you’re doing to be making another one to fit the new resized cell.