本文介绍了仅绘制uilabel周围的顶部,右侧和底部边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试为uilabel添加边框,但我只想要顶部,右侧和底部
边框。
i try to add border for a uilabel, but i only want to have top, right, and bottom
border.
喜欢这个
----------------
|
I am a label |
|
----------------
I尝试使用这些代码,但它默认添加所有4个边
I try to use these codes, but it adds all 4 sides by default
myLabel.layer.borderWidth = 1;
myLabel.layer.borderColor = [UIColor blackColor];
无论如何,我只能添加3个边,甚至1个或2个边?
Is that anyway i can only add 3 sides, or even 1 or 2 sides?
谢谢!
推荐答案
您可以使用面具。这是我用来测试理论的代码,效果很好:
You can use a mask. This is the code I used to test the theory, and it works well:
// 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;
您可以调整蒙版的大小和位置(给定borderWidth)以显示/隐藏边框你感兴趣的边缘。上面的例子隐藏了左边缘。
You can adjust the size and position of the mask (given the borderWidth) to show/hide the border edges you're interested in. The example above hides the left edge.
这篇关于仅绘制uilabel周围的顶部,右侧和底部边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!