【问题标题】:UILabel with layer Corner radius AND shadow带有图层角半径和阴影的 UILabel
【发布时间】:2019-04-26 07:54:20
【问题描述】:

我正在寻找一种方法来显示带有layer.cornerRadiuslayer.shadowUILabel

我发现,label.clipsToBounds = true 将设置 cornerRadiuslabel.masksToBounds = false 将显示阴影

两者都只显示阴影,不显示cornerRadius

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"

谁能解决这个问题,以便显示cornerRadiusshadow

【问题讨论】:

  • label label = UILabel() 还是let label = UILabel()
  • @1 是的,复制错误:/
  • 你在哪里设置它的框架?
  • @EmreÖnder 我尝试在设置框架后立即设置半径,但没有成功。你有解决办法吗?

标签: ios swift uikit uilabel


【解决方案1】:

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

【讨论】:

    【解决方案2】:
        label.layer.borderWidth = 0.2
        label.layer.borderColor = UIColor.clear.cgColor
        label.layer.shadowColor = UIColor.gray.cgColor
        label.layer.shadowOffset = CGSize(width: CGFloat(1.0), height: CGFloat(2.0))
        label.layer.shadowRadius = 1
        label.layer.shadowOpacity = 0.8
        label.layer.cornerRadius = 5.0
        label.layer.masksToBounds = false
    

    【讨论】:

      【解决方案3】:
      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.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-28
        • 2012-04-29
        • 1970-01-01
        • 1970-01-01
        • 2021-06-16
        • 2012-11-14
        • 2014-08-29
        相关资源
        最近更新 更多