只绘制一个uilabel的顶部,右侧和底部边框

我尝试为uilabel添加边框,但我只想要top, right, and bottom边框。

喜欢这个

  ---------------- | I am a label | | ---------------- 

我尝试使用这些代码,但它默认添加了所有4个边

  myLabel.layer.borderWidth = 1; myLabel.layer.borderColor = [UIColor blackColor]; 

无论如何,我只能添加三面,甚至一面或两面?

谢谢!

你可以使用一个面具。 这是我用来testing理论的代码,它运作良好:

 // 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)以显示/隐藏您感兴趣的边框边缘。上面的示例隐藏了左边缘。

你将不得不resize,但这是它的要点。 (可能是一些错别字)

 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]; 

您可以通过创build一个使用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]; 

我正在使用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是边框的宽度。

在这里,你们走了! 希望这有助于,添加这个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]]; 

你可以inheritanceUILabel并覆盖drawRect:来做到这一点。

或者你可以制作3个带有黑色背景的UIView作为这个标签的子视图。 如果你正确设置了autoresizingMask,他们会调整到标签大小的变化。 上边框的宽度应该有弹性,下边距要有弹性,下边框的宽度应该有弹性,顶边要有弹性,右边框应该有高度灵活的左边距。

你可以用多种方法做到这一点:第一个是最简单的find一个看起来像你的边框笔触的图像。 然后将它添加到你的UILabel的UIImageView。 第二种方法是覆盖UILabel drawRect:然后根据您的需要绘制中风。