NewDev
NewDev

Reputation: 257

UILabel with Border and background Issue

Here is the code I have for UILabel

descriptionText.layer.cornerRadius     = 8
descriptionText.layer.borderColor      = UIColor.lightGray.cgColor
descriptionText.layer.borderWidth      = 2
descriptionText.text                   = ""
descriptionText.layer.shadowOffset     = CGSize(width: -10, height: -10)
descriptionText.layer.shadowRadius     = -5.0
descriptionText.backgroundColor        = .white
descriptionText.textColor              = .black
descriptionText.numberOfLines          = 0
descriptionText.lineBreakMode          = .byWordWrapping

However This is what it looks likeenter image description here

Why is the Corner Not Transparent or how do you make what is outside the border transparent

Thanks in advance

Upvotes: 0

Views: 518

Answers (1)

Eman Harout
Eman Harout

Reputation: 536

You need to hide everything outside of the cornerRadius by setting .clipsToBounds on the label to true. Problem is that if you do this you will lose the shadow, as this is outside the bounds as well. Try this:

Place your label inside a container view, constrain the label to the container edges, and create an outlet to it. Then try the following code:

containerView.borderColor = UIColor.lightGray.cgColor
containerView.layer.borderColor = UIColor.lightGray.cgColor
containerView.layer.borderWidth = 2
containerView.layer.shadowOffset = CGSize(width: -10, height: -10)
containerView.layer.shadowRadius = -5.0
containerView.backgroundColor = UIColor.clear

descriptionText.text = "Testing"
descriptionText.cornerRadius = 8
descriptionText.clipsToBounds = true // or descriptionText.layer.masksToBounds = true
descriptionText.backgroundColor = .white
descriptionText.textColor = .black
descriptionText.numberOfLines = 0
descriptionText.lineBreakMode = .byWordWrapping

Upvotes: 1

Related Questions