我尝试为uilabel添加边框,但我只想拥有top, right, and bottom
边框。
----------------
|
I am a label |
|
----------------
我尝试使用这些代码,但默认情况下会添加所有4个边
myLabel.layer.borderWidth = 1;
myLabel.layer.borderColor = [UIColor blackColor];
无论如何,我只能添加3个边,甚至1个或2个边?
谢谢!
答案 0 :(得分: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;
您可以调整蒙版的大小和位置(给定borderWidth)以显示/隐藏您感兴趣的边框边缘。上面的示例隐藏了左边缘。
答案 1 :(得分:7)
你必须调整尺寸,但这是它的要点。 (可能是一些错别字)
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];
答案 2 :(得分:7)
您可以通过创建使用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];
答案 3 :(得分:5)
我正在使用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)
类似地:
// 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)
其中1是边框的宽度。
答案 4 :(得分:1)
你们走吧!希望这有帮助,在UItextfield覆盖类
中添加此内容UIView *view = [[UIView alloc] init];
view.translatesAutoresizingMaskIntoConstraints = NO;
view.layer.borderWidth = 1;
view.backgroundColor = [UIColor blackColor];
[self addSubview:view];
[self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:1.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:1.0]];
答案 5 :(得分:1)
感谢@jaiswal,我使用了你的代码并创建了一个函数,因为我有超过10个UILabel。我把它放在这里供参考。
func buttomBoarder(label: UILabel) -> UILabel {
let frame = label.frame
let bottomLayer = CALayer()
bottomLayer.frame = CGRect(x: 0, y: frame.height - 1, width: frame.width, height: 1)
bottomLayer.backgroundColor = UIColor.black.cgColor
label.layer.addSublayer(bottomLayer)
return label
}
使用
调用labelName = buttomBoarder(label: labelName)
答案 6 :(得分:0)
你可以继承UILabel并覆盖drawRect:来做到这一点。
或者你可以用黑色背景制作3个UIView作为这个标签的子视图。如果正确设置autoresizingMask,它们将根据标签大小的变化进行调整。 顶部边框应具有柔韧的宽度和柔韧的底边,底边应具有柔韧的宽度和柔韧的上边距,右边框应具有柔韧的高度和柔韧的左边距。
答案 7 :(得分:0)
您可以通过多种方式完成此操作: 第一个是最简单的只是找到一个看起来像你的边框笔画的图像。然后将它添加到UILabel的UIImageView背面。 第二种方式是覆盖UILabel drawRect:然后根据需要绘制冲程。