CALayer Border奇怪的问题

时间:2014-01-09 14:24:10

标签: ios objective-c border calayer

我正在为CALayer添加一个边框,但有一些奇怪的行为,我在我应用的边框之后得到一个模糊边框(见截图)

这是我的代码:

- (void)configureLabel {
self.hidden = YES;
self.textAlignment = NSTextAlignmentCenter;
self.font = kLightFontWithSize(12);
self.textColor = [UIColor whiteColor];
self.backgroundColor = [UIColor redColor];

self.clipsToBounds=YES;



CALayer * layer = self.layer;
layer.backgroundColor = [UIColor clearColor].CGColor;
layer.cornerRadius = self.frame.size.height / 2;
layer.borderWidth = 2.5f;
layer.borderColor = kTextFieldGreenColor.CGColor;
}

我的截图

enter image description here 感谢

2 个答案:

答案 0 :(得分:1)

看起来你有2.5点的边框宽度。根据设备的不同,可能会导致边缘模糊。

比2的偶数倍,看看会发生什么。

编辑

我刚刚测试了我的假设,这是错误的!这确实是一个奇怪的问题。我已经看到过去的半像素对齐问题,但它们总是具有大小为奇数像素的视图的功能,因此系统必须在边缘上绘制半像素,或者具有边框它并没有以像素完美的方式排列。

我的猜测是,当涉及到舍入视图时,这是不精确的浮点数学与绘图系统中的不精确相结合的函数。削波。

我唯一的建议是使用2视图方法,其中外部视图 - 在您的情况下 - 背景颜色(或您想要边框的颜色),内部视图是标签。您可以使外部视图略大于内部视图,并使每个视图都成圆形。

答案 1 :(得分:0)

我有同样的问题,这对我有用

myLayer.allowsEdgeAntialiasing = false

相关问题