2015-06-04 318 views
1

我想畫一個帶有文字的圓圈。無法顯示文字。任何幫助?使用CAShapeLayer填充顏色問題

下圖是參考。預期的行爲。

expected behaviour

以下是參考代碼:

UILabel *lblTitle = [[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)]; 
lblTitle.text = @"Me"; 
lblTitle.textColor = [UIColor blackColor]; 

lblTitle.font = [UIFont fontWithName:@"HelveticaNeueLight" size:10.0] ; 

lblTitle.textAlignment = NSTextAlignmentCenter; 
[view addSubview:lblTitle]; 

CAShapeLayer *circleLayer = [CAShapeLayer layer]; 
circleLayer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 40, 40)].CGPath; 
circleLayer.fillColor = [UIColor clearColor].CGColor; 
circleLayer.fillColor = [UIColor colorWithRed:56.0/255.0 green:212.0/255.0 blue:203.0/255.0 alpha:1.0f].CGColor; 
circleLayer.strokeColor = [UIColor blackColor].CGColor; 
circleLayer.lineWidth = 1; 
[lblTitle.layer addSublayer:circleLayer]; 
+3

您還可以通過嘗試設置'UILabel'的角落半徑 – Sujay

+0

您正在標籤頂部添加形狀圖層,而不是其他方式。 – stefandouganhyde

回答

2

爲@Sujay說,你可以設置的UILabel cornerRadius:

UILabel *lblTitle = [[UILabel alloc]initWithFrame:CGRectMake(100, 80, 40, 40)]; 
lblTitle.text = @"Me"; 
lblTitle.textColor = [UIColor blackColor]; 

lblTitle.layer.borderWidth = 1; 
lblTitle.layer.borderColor = [UIColor blackColor].CGColor; 
lblTitle.layer.cornerRadius = lblTitle.bounds.size.height/2; 
lblTitle.layer.masksToBounds = YES; 
lblTitle.backgroundColor = [UIColor colorWithRed:56.0/255.0 green:212.0/255.0 blue:203.0/255.0 alpha:1.0f]; 

lblTitle.font = [UIFont fontWithName:@"HelveticaNeueLight" size:10.0] ; 

lblTitle.textAlignment = NSTextAlignmentCenter; 
[self.view addSubview:lblTitle]; 

enter image description here

+0

完美地工作......謝謝你 – user2931321

+0

有沒有可能使用CAShapeLayer? – user2931321

+0

謝謝@Bannings提供的功勞,你也可以贊成我的評論。 – Sujay