Jul*_* B. 14 objective-c uiview uilabel ios
我正在创建一个UILabel,我使用以下代码设置背景颜色和角半径:
self.scoreLabel.backgroundColor = [UIColor DISRed];// custom red`
self.scoreLabel.layer.masksToBounds = YES;
self.scoreLabel.layer.cornerRadius = self.scoreLabel.frame.size.width/2;
self.scoreLabel.layer.borderWidth = 8.0;
self.scoreLabel.layer.borderColor = [[UIColor DISNavy] CGColor];
Run Code Online (Sandbox Code Playgroud)
然而,背景的颜色似乎泄漏到边界的边缘(见图).有什么想法吗?有关如何解决它的任何想法?
我也面临同样的问题。这是一个愚蠢的错误。我总是忘记clipToBounds
在 的情况下打勾cornerRadius
。
因此,只需UILabel
在 Storyboard 中勾选 Clip to Bounds 即可解决我的问题。
是的,我们也需要保留以下代码:
label.layer.masksToBounds = true
Run Code Online (Sandbox Code Playgroud)
我创建了自己的 UILabel,背景颜色似乎没有泄漏。
将其写入.h
您的项目文件中。
UILabel *label;
将其写入.m
您的项目文件中。
label=[[UILabel alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];//Set frame of label in your viewcontroller.
[label setBackgroundColor:[UIColor redColor]];//Set background color of label.
[label setText:@"Label"];//Set text in label.
[label setTextColor:[UIColor blackColor]];//Set text color in label.
[label setTextAlignment:NSTextAlignmentCenter];//Set text alignment in label.
[label.layer setCornerRadius:50.0];//Set corner radius of label to change the shape.
[label.layer setBorderWidth:8.0f];//Set border width of label.
[label setClipsToBounds:YES];//Set its to YES for Corner radius to work.
[label.layer setBorderColor:[UIColor greenColor].CGColor];//Set Border color.
[self.view addSubview:label];//Add it to the view of your choice.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3035 次 |
最近记录: |