Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is UILabel Inset effect Possible? [duplicate]

I am giving border to UILabel with

Label.text = lbltext;
Label.layer.borderColor = [[UIColor grayColor] CGColor];
Label.layer.borderWidth = 2;

But There is no space between text and border.
so how can i set inset effect like UIButton in my Label?

like image 532
iUser Avatar asked Jul 28 '11 06:07

iUser


2 Answers

Put the label in a container view and apply the border to the container.

like image 75
Benjamin Mayo Avatar answered Oct 05 '22 02:10

Benjamin Mayo


You can subclass UILabel, and override a couple of methods:

The first gives you rounded corners and a border. You can tweak the border width, color etc. as needed.

- (void)drawRect:(CGRect)rect
{
    self.layer.cornerRadius = 4.0;
    self.layer.borderWidth = 1;

    [super drawRect:rect];
}

The second lets you specify insets to position the label text away from the left border.

- (void) drawTextInRect:(CGRect)rect
{   
    UIEdgeInsets insets = {0,5,0,5};

    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
like image 22
northernman Avatar answered Oct 05 '22 04:10

northernman