Code Wiget
Code Wiget

Reputation: 1682

Swift - Setting a label border using border width and color is working, but setting when setting a specific side the frame width is off

I am trying to put a border around my labels just on certain sides so that they do not overlap. Here is the code I am using: cell.jobAddress.layer.borderColor =

`cell.jobAddress.layer.borderColor = UIColor.black.cgColor
        cell.jobAddress.layer.borderWidth = 2
        cell.jobPlaceBid.layer.borderColor = UIColor.black.cgColor
        cell.jobPlaceBid.layer.borderWidth = 2
`

and here is what it looks like: enter image description here

As you can see, the bottoms and tops overlap. So, I have been using this code to try to add borders only to specific sides :

    extension CALayer {

    func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {

        let border = CALayer()

        switch edge {
        case UIRectEdge.top:
            border.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: thickness)
            break
        case UIRectEdge.bottom:
            border.frame = CGRect(x: 0, y: self.frame.height - thickness, width: self.frame.width, height: thickness)
            break
        case UIRectEdge.left:
            border.frame = CGRect(x: 0, y: 0, width: thickness, height: self.frame.height)
            break
        case UIRectEdge.right:
            border.frame = CGRect(x: self.frame.width - thickness, y: 0, width: thickness, height: self.frame.height)
            break
        default:
            break
        }

        border.backgroundColor = color.cgColor;

        self.addSublayer(border)
    }

}

and then this to implement it:

cell.jobAddress.layer.addBorder(edge: UIRectEdge.left, color: UIColor.black, thickness: 2)
    cell.jobAddress.layer.addBorder(edge: UIRectEdge.right, color: UIColor.black, thickness: 2)
    cell.jobAddress.layer.addBorder(edge: UIRectEdge.top, color: UIColor.black, thickness: 2)
    cell.jobAddress.layer.addBorder(edge: UIRectEdge.bottom, color: UIColor.black, thickness: 2)
    cell.jobPlaceBid.layer.addBorder(edge: UIRectEdge.left, color: UIColor.black, thickness: 2)
    cell.jobPlaceBid.layer.addBorder(edge: UIRectEdge.right, color: UIColor.black, thickness: 2)
    cell.jobPlaceBid.layer.addBorder(edge: UIRectEdge.bottom, color: UIColor.black, thickness: 2)

Here is what this looks like:

enter image description here

What do you think could be wrong? I wait until the labels have values before adding the borders. I set the width just by making the labels 10 points from each side.

Thank you in advance, I know this is a long post!

Upvotes: 1

Views: 2555

Answers (2)

RajeshKumar R
RajeshKumar R

Reputation: 15768

Auto layout hasn't had the time to layout your views by the time you call addBorder.

cell.setNeedsLayout()
cell.layoutIfNeeded()

forces a calculation of UILabel dimensions causing the expected values to get in addBorder method.

Upvotes: 1

Code Wiget
Code Wiget

Reputation: 1682

@RaheshkumarR answered the question in the comments, I am only posting it here for visibility when others run into the problem. The solution is to use cell.mylabel.setNeedsLayout() and cell.mylabel.layoutIfNeeded().

I hope this helps!

Upvotes: 0

Related Questions