【发布时间】:2016-11-16 12:28:05
【问题描述】:
您好,我正在尝试使用 UILabel 图层绘制一个圆圈,但是,我的标签仅显示数字,而不是我为标签创建的背景颜色或边框。为什么是这样 ? 这是我的代码:
let countLabel = UILabel()
countLabel.text = "5"
let size:CGFloat = 55.0
countLabel.textColor = UIColor.white
countLabel.textAlignment = .center
countLabel.font = UIFont.systemFont(ofSize: 14.0)
countLabel.bounds = CGRect(x : 0.0,y : 0.0,width : size, height : size)
countLabel.layer.cornerRadius = size / 2
countLabel.layer.borderWidth = 3.0
//countLabel.layer.masksToBounds = true
countLabel.layer.backgroundColor = UIColor.orange.cgColor
countLabel.layer.borderColor = UIColor.orange.cgColor
countLabel.center = CGPoint(x:200.0,y: 200.0)
countLabel.translatesAutoresizingMaskIntoConstraints = false
self.topContainer.addSubview(countLabel)
countLabel.topAnchor.constraint(equalTo: profileImage.topAnchor).isActive = true
countLabel.trailingAnchor.constraint(equalTo: profileImage.trailingAnchor, constant: 10).isActive = true
我想要这样的东西。
但是上面没有输出橙色。为什么?
【问题讨论】:
-
你试过将
clipsToBound设置成true吗? -
我试过 clipstobound,我的标签根本不显示
-
取消注释
countLabel.layer.masksToBounds = true并使用countLabel.backgroundColor = .orange设置标签的backgroundColor