UILabel有边框和背景问题

时间:2018-06-15 19:31:05

标签: swift xcode uilabel

以下是我为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

然而,这就是enter image description here

的样子

为什么角落不透明或者如何使边界外的内容透明

提前致谢

1 个答案:

答案 0 :(得分:1)

您需要通过将标签上的.clipsToBounds设置为true来隐藏cornerRadius之外的所有内容。问题是,如果你这样做,你将失去阴影,因为这也超出了界限。试试这个:

将标签放在容器视图中,将标签约束到容器边缘,然后为其创建出口。然后尝试以下代码:

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