1

I am using custom TableCell which has a label with numberofLines = 0

I wanted the label like this.

enter image description here

That is rounded only in three corners.For this to achieve i've written code like this in cell's swift file.I've extended the UIView basically.

extension UIView {
    func roundCorners(corners:UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}

And i've used this like this to round my corners.

self.chatLabel.roundCorners(corners: [.topLeft,.bottomLeft,.bottomRight], radius: 7.0)

But the problem is when label's content is bigger than the cell's row height it's not showing the content

Please just see the second cell's label in first picture .It's content is not showing. But when i remove the rounded corner's code everything is working fine but lost my corner radius.

enter image description here

Any suggestion is highly appreciated.Thanks.

Edited:

My cell is like this.

enter image description here

And i am achieving chat UI using the code below.

var cellTypeToDraw:cellType {
        get {
            return self.cellTypeToDraw
        }
        set(newValue) {
            switch newValue {
            case .cellTypeReceiver:
                self.leftImage.isHidden = true
                 self.rightImage.isHidden = false
                self.chatLabel.textAlignment = .right
                self.chatLabel.roundCorners(corners: [.topLeft,.bottomLeft,.bottomRight], radius: 7.0)
            case .cellTypeSender:
                self.rightImage.isHidden = true
                self.leftImage.isHidden = false
                self.chatLabel.textAlignment = .left
                self.chatLabel.roundCorners(corners: [.topRight,.bottomLeft,.bottomRight], radius: 7.0)
            }
            self.leftImage.layer.cornerRadius = self.leftImage.bounds.size.width/2
            self.rightImage.layer.cornerRadius = self.rightImage.bounds.size.width/2
           // self.chatLabel.sizeToFit()
           // self.chatLabel.autoresizingMask = .flexibleHeight
        }
    }

and accessing this variable in cellForRowAtIndexPath like this.

 if indexPath.row % 2 != 0 {
            cell.cellTypeToDraw = .cellTypeReceiver
        }
        else {
            cell.cellTypeToDraw = .cellTypeSender
        }
elk_cloner
  • 2,049
  • 1
  • 12
  • 13

2 Answers2

1

You're doing it too early.

You set the cellTypeToDraw and thus call your roundCorners(corners:, radius:) method at a point in time when the layout isn't done yet. In cellForRow(at:) you get a recycled cell whose chatLabel still has the frame that matched the text it displayed previously.

A quick fix would be to move this corner-rounding to the cell's layoutSubviews() method. Something like:

override func layoutSubviews() {
    super.layoutSubviews()
    chatLabel.roundCorners(corners: [.topLeft,.bottomLeft,.bottomRight], radius: 7.0)
}

(You can check the cellTypeToDraw there as well if you only want to have rounded corners for some cell types.)


By the way:

Tip 1:

You should never do something like that:

var cellTypeToDraw:cellType {
    get {
        return self.cellTypeToDraw
    }
    // ...
}

This will result in an endless recursion when you ever try to call cellTypeToDraw and eventually crash your app. By implementing the getter and the setter you define a computed property that is not backed by an instance variable. self.cellTypeToDraw doesn't have a value. It simply calls the getter again which returns self.cellTypeToDraw again and so on.

Solution: Don't use a computed property but a "regular" one which is backed by an instance variable and do all your cell setup in the property's observer:

var cellTypeToDraw:cellType {
    didSet {
        // configure your cell here
    }
}

Tip 2:

You can (and should) omit all the self. prefixes in Swift for better readability and less work.

Mischa
  • 15,816
  • 8
  • 59
  • 117
-2

Add a view insert your label and add some space between your label and that view.

vaibby
  • 1,255
  • 1
  • 9
  • 23