ios – 只绘制一个uilabel周围的顶部,右侧和底部边界

我尝试为uilabel添加边框,但我只想要有顶部,右边和底部边框.

喜欢这个

----------------
                          |
            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,view.frame.size.width - borderWidth,view.frame.size.height)];
mask.backgroundColor = [UIColor blackColor];
view.layer.mask = mask.layer;

您可以调整面具的大小和位置(给出borderWidth)以显示/隐藏您感兴趣的边框边缘.上面的示例隐藏了左边缘.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...