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

Ios 带有层角半径和阴影的UILabel,ios,swift,uikit,uilabel,Ios,Swift,Uikit,Uilabel,我正在寻找一种显示带有layer.cornerRadius和layer.shadow的UILabel的方法 我发现,如果label.clipsToBounds=true,将设置拐角半径,如果label.masksToBounds=false,将显示阴影 let label = UILabel() label.textAlignment = .center label.font = UIFont.systemFont(ofSize: 32, weight: .regular) label.text

我正在寻找一种显示带有layer.cornerRadius和layer.shadow的UILabel的方法

我发现,如果label.clipsToBounds=true,将设置拐角半径,如果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"
两种情况下,只显示阴影,不显示拐角半径

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和阴影吗?

为什么不尝试为标签添加一个包含背景色和cornerRadius的父UIView。然后将阴影特性保留到标签

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


是label label=UILabel还是let label=UILabel?可能重复@1 sry是的,复制错误:/u在哪里设置其框架?@EmreÖnder我在设置框架后尝试设置半径,但没有成功。你有解决办法吗?