Search code examples
iosuilabelcalayer

iOS add bottom border to UILabel with shadow


May i know how do i add bottom border to UILable only with shadow? No top, left, right border.

this is not working. Here is the code I've tried but it's not working.

CALayer* layer = [titleLabel layer];
layer.frame = CGRectMake(-1, -1, titleLabel.frame.size.width, 1.0f);
layer.borderWidth=1;
[layer setBorderWidth:2.0f];
[layer setBorderColor:[UIColor blackColor].CGColor];
[layer setShadowOffset:CGSizeMake(-3.0, 3.0)];
[layer setShadowRadius:5.0];
[layer setShadowOpacity:5.0];

Solution

  • Test in this way:

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

    I hope this helps you