我尝试为 uilabel 添加边框,但我只想有 top、right 和 bottom
边框。
像这样
----------------
|
I am a label |
|
----------------
我尝试使用这些代码,但它默认添加了所有 4 个面
myLabel.layer.borderWidth = 1;
myLabel.layer.borderColor = [UIColor blackColor];
无论如何我只能添加 3 个边,甚至是 1 个或 2 个边?
谢谢!
最佳答案
你可以使用面具。这是我用来测试理论的代码,效果很好:
// Define the border width in a variable, we'll be using it elsewhere
CGFloat borderWidth = 1.0;
// This creates a testing view to test the theory, in your case this will be your UILabel
UIView* view = [[UIView alloc] initWithFrame:CGRectMake(20, 60, 250, 100)];
view.layer.borderColor = [UIColor blackColor].CGColor;
view.layer.borderWidth = borderWidth;
[self.view addSubview:view];
// Create the mask to cover the area of the view you want to **show**
// Here, we create a mask that covers most of the view, except the left edge
// The mask needs to be coloured in black, as black acts as transparent, whereas white is opaque in mask parlance
UIView* mask = [[UIView alloc] initWithFrame:CGRectMake(borderWidth, 0, view.frame.size.width - borderWidth, view.frame.size.height)];
mask.backgroundColor = [UIColor blackColor];
view.layer.mask = mask.layer;
您可以调整 mask 的大小和位置(给定 borderWidth)以显示/隐藏您感兴趣的边框边缘。上面的示例隐藏了左边缘。
关于ios - 仅在 uilabel 周围绘制顶部、右侧和底部边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21586089/