UILabel: 如何可以更改除底部外的所有边框?

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值