【问题标题】:Using CAShapeLayer fill color issue使用 CAShapeLayer 填充颜色问题
【发布时间】:2015-06-04 11:12:53
【问题描述】:

我想画一个圆圈,里面有文字。无法显示文字。有什么帮助吗?

下图是参考。预期行为。

以下是参考代码:

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];

【问题讨论】:

  • 你也可以设置UILabel的cornerRadius试试
  • 您是在标签顶部添加形状图层,而不是相反。

标签: ios objective-c iphone cashapelayer


【解决方案1】:

你可以像@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];

【讨论】:

  • 可以使用 CAShapeLayer 吗?
  • 感谢@Bannings 给予信任,您也可以为我的评论投票。
  • @Sujay 可以使用 CAShapeLayer 吗?
  • @user2931321 你可以使用 CATextLayer
  • @user2931321,是的,这是可能的,但需要检查,因为我实际上没有做过。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多