UILabel插入效果可能吗?

iUs*_*ser 6 iphone uilabel xcode4

我正在给UILabel边境

Label.text = lbltext;
Label.layer.borderColor = [[UIColor grayColor] CGColor];
Label.layer.borderWidth = 2;
Run Code Online (Sandbox Code Playgroud)

但是文本和边框之间没有空格.
那么如何在我的标签中设置像UIButton这样的插入效果呢?

Ben*_*ayo 20

将标签放在容器视图中,并将边框应用于容器.


nor*_*man 18

您可以继承UILabel,并覆盖以下几种方法:

第一个为您提供圆角和边框.您可以根据需要调整边框宽度,颜色等.

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

    [super drawRect:rect];
}
Run Code Online (Sandbox Code Playgroud)

第二个允许您指定插入以将标签文本放置在远离左边框的位置.

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

    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
Run Code Online (Sandbox Code Playgroud)