0

Hi I used the the following code to create a bottom border to my label

CALayer *border = [CALayer layer];
    CGFloat borderWidth = 2;
    border.borderColor = [UIColor darkGrayColor].CGColor;
    border.frame = CGRectMake(0, _label.frame.size.height - borderWidth, _label.frame.size.width, _label.frame.size.height);
    border.borderWidth = borderWidth;
    [_label.layer addSublayer:border];
    _label.layer.masksToBounds = YES;

But it the border is breaks at half of label. How can I fix it. Thanks in advance.

e.k
  • 1,333
  • 1
  • 17
  • 36
  • 1
    Boss it is working for me without any breaking. Can u attach the screen shot? – MuraliMohan Nov 17 '16 at 12:50
  • Possible duplicate of [CALayer: add a border only at one side](http://stackoverflow.com/questions/7022656/calayer-add-a-border-only-at-one-side) – sage444 Nov 17 '16 at 13:06
  • @MuraliMohan It is working for me too but border is not fully drawn. – e.k Nov 17 '16 at 13:56

4 Answers4

2

You can do like this :

With the use of CALayer you can create border on UILabel or any other UIControl.

1) Bottom Border

 CALayer *bottomBorder = [CALayer layer];
 bottomBorder.borderColor = [UIColor blackColor].CGColor;
 bottomBorder.borderWidth = 1;
 bottomBorder.frame = CGRectMake(0, CGRectGetHeight(myLabel.frame)-1, CGRectGetWidth(myLabel.frame), 1);
 myLabel.clipsToBounds = YES;
 [myLabel.layer addSublayer:bottomBorder];

2) Top Border

 CALayer *topBorder = [CALayer layer];
 topBorder.borderColor = [UIColor blackColor].CGColor;
 topBorder.borderWidth = 1;
 topBorder.frame = CGRectMake(0, 0, CGRectGetWidth(myLabel.frame), 1);
 myLabel.clipsToBounds = YES;
 [myLabel.layer addSublayer:topBorder];

3) Left Border

 CALayer *leftBorder = [CALayer layer];
 leftBorder.borderColor = [UIColor blackColor].CGColor;
 leftBorder.borderWidth = 1;
 leftBorder.frame = CGRectMake(0, 0, 1, CGRectGetHeight(myLabel.frame));
 myLabel.clipsToBounds = YES;
 [myLabel.layer addSublayer:leftBorder];

4) Right Border

 CALayer *rightBorder = [CALayer layer];
 rightBorder.borderColor = [UIColor blackColor].CGColor;
 rightBorder.borderWidth = 1;
 rightBorder.frame = CGRectMake(CGRectGetWidth(myLabel.frame)-1, 0, 1, CGRectGetHeight(myLabel.frame));
 myLabel.clipsToBounds = YES;
 [myLabel.layer addSublayer:rightBorder];
sohil
  • 818
  • 2
  • 15
  • 38
1

Try it,

    CALayer* layer = [lbl layer];
    CALayer *bottomBorder = [CALayer layer];
    bottomBorder.borderColor = [UIColor darkGrayColor].CGColor;
    bottomBorder.borderWidth = 1;
    bottomBorder.frame = CGRectMake(-1, layer.frame.size.height-1,layer.frame.size.width, 1);
    [bottomBorder setBorderColor:[UIColor blackColor].CGColor];
    [layer addSublayer:bottomBorder];

check demo

Ashish Sahu
  • 388
  • 3
  • 16
  • This also draws to half of the label. – e.k Nov 17 '16 at 13:57
  • No dude, it's draw actual width of the label, by this code CALayer* layer = [lbl layer]; bottomBorder.frame = CGRectMake(-1, layer.frame.size.height-1,layer.frame.size.width, 1) see screenshot also. – Ashish Sahu Nov 18 '16 at 05:10
0

You need to calculate width of label based on string and also font which you require. You can get size like this

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font 
                    constrainedToSize:maximumLabelSize 
                    lineBreakMode:yourLabel.lineBreakMode]; 

and use expectedLabelSize while setting frame for your border.

border.frame = CGRectMake(0, _label.frame.size.height - borderWidth, _expectedLabelSize.width , _label.frame.size.height);

Hope it helps. Happy Coding !!

luckyShubhra
  • 2,731
  • 1
  • 12
  • 19
0

Please try with the following code.

UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];

[lbl setText:@"Testo di prova..."];

[lbl setBackgroundColor:[UIColor clearColor]];

[[self view] addSubview:lbl];

[lbl sizeToFit];

CALayer* layer = [lbl layer];

CALayer *bottomBorder = [CALayer layer];

bottomBorder.borderColor = [UIColor darkGrayColor].CGColor;

bottomBorder.borderWidth = 1;

bottomBorder.frame = CGRectMake(-1, layer.frame.size.height-1, layer.frame.size.width, 1);

[bottomBorder setBorderColor:[UIColor blackColor].CGColor];

[layer addSublayer:bottomBorder];

Hiroki Murahi
  • 270
  • 1
  • 5