带有层转角半径和阴影的UILabel

时间:2019-04-26 07:54:20

标签: ios swift uikit uilabel

我正在寻找一种显示带有UILabellayer.cornerRadius的{​​{1}}的方法。

我发现,使用layer.shadow会设置label.clipsToBounds = true,使用cornerRadius会显示阴影

仅显示阴影,不显示cornerRadius

label.masksToBounds = false

任何人都可以解决此问题,以便显示let label = UILabel() label.textAlignment = .center label.font = UIFont.systemFont(ofSize: 32, weight: .regular) label.textColor = .white label.clipsToBounds = true label.backgroundColor = Colors.Vibrants.softBlue label.layer.cornerRadius = 50 label.layer.masksToBounds = false label.layer.shadowColor = UIColor.black.cgColor label.layer.shadowOffset = CGSize(width: 5, height: 5) label.layer.shadowRadius = 5 label.layer.shadowOpacity = 0.7 label.text = "0" cornerRadius吗?

3 个答案:

答案 0 :(得分:1)

为什么不尝试为标签添加父项 "<img width=\"" + pixelPerCm + "\" height=\"" + pixelPerCm + "\" src=\"" + imagePath + "\" " + sStyle + ">" ,该父项将包含背景颜色和cornerRadius。然后将阴影属性保留到标签上

答案 1 :(得分:1)

URLSessionDidFinishEventsForBackgroundURLSession

答案 2 :(得分:0)

class setShadowOnLabel: UILabel
{
    override func layoutSubviews()
    {
        self.layer.cornerRadius = 5
        self.layer.shadowColor = UIColor.lightGray.cgColor
        self.layer.shadowOffset = CGSize(width: 0.0, height: 0.2)
        self.layer.shadowOpacity = 0.80
        self.layer.shadowRadius = 5.0
        self.layer.masksToBounds = false
    }
}

然后将setShadowOnLabel类直接分配给Label。enter image description here

enter image description here