我试图为uilabel添加边框,但我只想要top, right, and bottom边框.
像这样
----------------
|
I am a label |
|
----------------
Run Code Online (Sandbox Code Playgroud)
我尝试使用这些代码,但默认情况下会添加所有4个边
myLabel.layer.borderWidth = 1;
myLabel.layer.borderColor = [UIColor blackColor];
Run Code Online (Sandbox Code Playgroud)
无论如何,我只能添加3个边,甚至1个或2个边?
谢谢!
WDU*_*DUK 28
你可以使用面具.这是我用来测试理论的代码,效果很好:
// 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;
Run Code Online (Sandbox Code Playgroud)
您可以调整蒙版的大小和位置(给定borderWidth)以显示/隐藏您感兴趣的边框边缘.上面的示例隐藏了左边缘.
你必须调整尺寸,但这是它的要点.(可能是一些错别字)
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 35)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 320, 20)];
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0, 40, 320, .5);
CALayer *rightBorder = [CALayer layer];
rightBorder.frame = CGRectMake(320, 0, 40, .5);
CALayer *topBorder = [CALayer layer];
topBorder.frame = CGRectMake(0, 0, 320, .5);
[view.layer addSublayer:bottomBorder];
[view.layer addSublayer:topBorder];
[view.layer addSublayer:rightBorder];
[view.layer addSublayer:label];
Run Code Online (Sandbox Code Playgroud)
您可以通过创建使用UIBezierPath的CALayer来绘制三条线.通过所有示例,将QuartzCore框架作为项目的一部分.
以原始代码为出发点:
// at the top of the file with this code, include:
#import <QuartzCore/QuartzCore.h>
CGRect rect = myLabel.frame;
UIBezierPath * linePath = [UIBezierPath bezierPath];
// start at top left corner
[linePath moveToPoint:CGPointMake(0,0);
// draw top line across
[linePath addLineToPoint:CGPointMake(rect.size.width, 0);
// draw right vertical side
[linePath addLineToPoint:CGPointMake(rect.size.width, rect.size.height);
// draw left vertical side
[linePath addLineToPoint:CGPointMake(0, rect.size.height);
// draw from bottom right corner back to bottom left corner
[linePath addLineToPoint:CGPointMake(0, rect.size.height);
// create a layer that uses your defined path
CAShapeLayer * lineLayer = [CAShapeLayer layer];
lineLayer.lineWidth = 1.0;
lineLayer.strokeColor = [UIColor blackColor].CGColor;
lineLayer.fillColor = nil;
lineLayer.path = linePath.CGPath;
[myLabel.layer addSublayer:lineLayer];
Run Code Online (Sandbox Code Playgroud)
我正在使用Swift 3.
// myLabel is a UILabel
let frame = myLabel.frame //Frame of label
// Bottom Layer
let bottomLayer = CALayer()
bottomLayer.frame = CGRect(x: 0, y: frame.height - 1, width: frame.width, height: 1)
bottomLayer.backgroundColor = UIColor.black.cgColor
myLabel.layer.addSublayer(bottomLayer)
// Top Layer
let topLayer = CALayer()
topLayer.frame = CGRect(x: 0, y: 0, width: frame.width, height: 1)
topLayer.backgroundColor = UIColor.black.cgColor
myLabel.layer.addSublayer(topLayer)
Run Code Online (Sandbox Code Playgroud)
同理:
// Right Layer
let rightLayer = CALayer()
rightLayer.frame = CGRect(x: frame.width - 1, y: 0, width: 1, height: frame.height)
rightLayer.backgroundColor = UIColor.black.cgColor
myLabel.layer.addSublayer(rightLayer)
Run Code Online (Sandbox Code Playgroud)
其中1是边界的宽度.
| 归档时间: |
|
| 查看次数: |
17023 次 |
| 最近记录: |