UILabel背景颜色泄漏到边框 [英] UILabel Background Color Leaks to Border

查看:66
本文介绍了UILabel背景颜色泄漏到边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个UILabel,使用以下代码将其设置为背景颜色和拐角半径:

I'm creating a UILabel to which I set the background color and corner radius with the following code:

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

但是,背景颜色似乎泄漏到边框的边缘(参见图片).有什么想法吗?关于如何解决它的任何想法?

However the background's color seems to be leaking to the edge of the border (see image). Any ideas why? Any idea on how to fix it?

推荐答案

我也遇到了同样的问题.这是一个愚蠢的错误.如果遇到 cornerRadius ,我总是忘了勾选 clipToBounds .

I was also facing the same problem. It was a silly mistake. I always forget to tick clipToBounds in case of cornerRadius.

因此,只需在Storyboard中为 UILabel 的剪辑范围"打勾,即可解决我的问题.

So, just ticking the Clip to Bounds for UILabel in Storyboard fixed my problem.

是的,我们也需要保留以下代码:

And yes, we need to keep the below code too:

label.layer.masksToBounds = true

这篇关于UILabel背景颜色泄漏到边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆