UILabel: 如何可以更改除底部外的所有边框?
我知道我可以改变边框的对象
item.layer.cornerRadius = floatValue;
item.layer.borderWidth = intValue;
item.layer.borderColor = colorValue;
但如何可以只更改上、 左和右边框?
谢谢您的宝贵建议。
解决方法 1:
您可以使用另一个层口罩走到你不想看到的角落。这有缺点,您:
- 不能有阴影
- 不能有另一个掩码 (如果你不能一起做他们)
- 由于边界抚摸你边界的中心将失去一半的边框宽度
如果是你,这里是应该让你开始的示例代码
CGFloat borderWidth = 4.0;
[[myView layer] setBorderWidth:borderWidth];
CALayer *mask = [CALayer layer];
// The mask needs to be filled to mask
[mask setBackgroundColor:[[UIColor blackColor] CGColor]];
// Make the masks frame smaller in height
CGRect maskFrame = CGRectInset([myView bounds], 0, borderWidth);
// Move the maskFrame to the top
maskFrame.origin.y = 0;
[mask setFrame:maskFrame];
[[myView layer] setMask:mask];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 100, 100)];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(200, 400, 100, 100)];
label1.backgroundColor = [UIColor blueColor];
[self.view addSubview:label1];
label.backgroundColor = [UIColor cyanColor];
[self.view addSubview:label];
label.layer.borderWidth = borderWidth;
label.layer.borderColor = [UIColor redColor].CGColor;
CALayer *mask = [CALayer layer];
// The mask needs to be filled to mask
[mask setBackgroundColor:[[UIColor blackColor] CGColor]];
// Make the masks frame smaller in height
CGRect maskFrame = CGRectInset([label bounds], 0, borderWidth/2);
// Move the maskFrame to the top
// maskFrame.origin.y = 0;
// maskFrame.origin.y = borderWidth*2;
maskFrame.origin.y = borderWidth;
[mask setFrame:maskFrame];
[[label layer] setMask:mask];