2016-11-16 84 views
2

嗨我想通過使用圖層爲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   

我想得到這樣的東西。

enter image description here

但上述不輸出橙色。爲什麼?

+0

你有沒有試過將'clipsToBound'設置爲'true'? – Rajat

+0

我試過clipstobound,我的標籤根本沒有顯示 – Aboogie

+0

取消'countLabel.layer.masksToBounds = true'並使用'countLabel.backgroundColor = .orange'設置標籤的backgroundColor –

回答

-2

創建的視圖,並使它一個圓,然後加入內側的標籤。這裏是代碼:

let size:CGFloat = 36 
     let someView = UIView() 
     someView.translatesAutoresizingMaskIntoConstraints = false 
     someView.layer.cornerRadius = size/2 
     someView.backgroundColor = .orange 
     someView.layer.borderColor = UIColor.white.cgColor 
     someView.layer.borderWidth = 2 

     self.topContainer.addSubview(someView) 

     someView.topAnchor.constraint(equalTo: profileImage.topAnchor).isActive = true 
     someView.trailingAnchor.constraint(equalTo: profileImage.trailingAnchor, constant: 10).isActive = true 
     someView.heightAnchor.constraint(equalToConstant: size).isActive = true 
     someView.widthAnchor.constraint(equalToConstant: size).isActive = true 

     let countLabel = UILabel() 
     countLabel.text = "5" 
     countLabel.textColor = UIColor.white 
     countLabel.textAlignment = .center 
     countLabel.font = UIFont.systemFont(ofSize: 14.0) 

     countLabel.translatesAutoresizingMaskIntoConstraints = false 
     someView.addSubview(countLabel) 

     countLabel.centerXAnchor.constraint(equalTo: someView.centerXAnchor).isActive = true 
     countLabel.centerYAnchor.constraint(equalTo: someView.centerYAnchor).isActive = true 
2

我想你偶然發現了一個bug。

如果我將代碼添加到遊樂場中,它會輸出「空白圖像」而不是顯示視圖。

但是,如果我創建一個框架的標籤,它的作品。

不工作:

let countLabel = UILabel() 
countLabel.frame = CGRect(x : 0.0,y : 0.0,width : size, height : size) 

不工作:

let countLabel = UILabel() 
countLabel.bounds = CGRect(x : 0.0,y : 0.0,width : size, height : size) 

不工作:

let countLabel = UILabel() 
countLabel.sizeToFit() 

工作:

let size:CGFloat = 55.0 
let countLabel = UILabel(frame: CGRect(x : 0.0,y : 0.0,width : size, height : size)) 
從我的操場

全碼:

import UIKit 
let size:CGFloat = 55.0 

let countLabel = UILabel(frame: CGRect(x : 0.0,y : 0.0,width : size, height : size)) 
countLabel.text = "5" 
countLabel.textColor = UIColor.white 
countLabel.textAlignment = .center 

countLabel.font = UIFont.systemFont(ofSize: 24.0) 
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.translatesAutoresizingMaskIntoConstraints = false 

結果:

enter image description here

3

嘗試添加下面的代碼:

countLabel.layer.cornerRadius = 0.5 * countLabel.bounds.size.width 

相反的:

// remove the below code of line from your code 
countLabel.layer.cornerRadius = size/2 
+0

標籤的框架是使用'size'創建的。這兩個代碼是相同的。 – vikingosegundo

+0

我尊重你的話在這裏,但幸運的是這裏返回的值使差異。 – vaibhav

0

與代碼:

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.frame = CGRect(x : 50.0,y : 50.0,width : size, height : size) 
countLabel.layer.cornerRadius = size/2 
countLabel.layer.borderWidth = 3.0 
countLabel.layer.backgroundColor = UIColor.orange.cgColor 
countLabel.layer.borderColor = UIColor.orange.cgColor 


self.view.addSubview(countLabel) 

我得到這個結果:

enter image description here

+0

在Xcode 8.1中,我在操場上用這段代碼得到「空白圖像」。 – vikingosegundo

+0

但沒有涉及圖像?! – ben

1

刪除約束上,並設置countLabel.center您的容器意見中心。經測試,在遊樂場:

import UIKit 
import PlaygroundSupport 

let container = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0)) 

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 = container.center 
countLabel.translatesAutoresizingMaskIntoConstraints = false 
container.addSubview(countLabel) 
//countLabel.topAnchor.constraint(equalTo: countLabel.topAnchor).isActive = true 
//countLabel.trailingAnchor.constraint(equalTo: countLabel.trailingAnchor, constant: 10).isActive = true 

PlaygroundPage.current.liveView = container 
0

試試這個

countLabel.layer.masksToBounds = YES; 
countLabel.layer.cornerRadius = 17.5;//this should half of you laber layer 
countLabel.backgroundColor = [ViewController colorFromHexString:@"#f36f34"]; 
countLabel.text = [NSString stringWithFormat:@"%d",CartItemCount]; 
countLabel.font = [UIFont fontWithName:@"Lato-Black" size:15.0]; 
countLabel.textColor = [ViewController colorFromHexString:@"#ffffff"]; 
+0

sagar它的'Swift'標記的問題。 – vaibhav

+0

你可以使用在線轉換器轉換。 –