嗨我想通過使用圖層爲UILabel繪製一個圓圈,但是,我的標籤只顯示數字而不是我爲標籤創建的背景顏色或邊框。爲什麼是這樣 ? 這是我的代碼:iOS標籤在一個圓圈
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'? – Rajat
我試過clipstobound,我的標籤根本沒有顯示 – Aboogie
取消'countLabel.layer.masksToBounds = true'並使用'countLabel.backgroundColor = .orange'設置標籤的backgroundColor –