6

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?

user513951
  • 12,445
  • 7
  • 65
  • 82
iUser
  • 1,075
  • 3
  • 20
  • 49

4 Answers4

20

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

Benjamin Mayo
  • 6,649
  • 2
  • 26
  • 25
18

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)];
}
northernman
  • 1,446
  • 16
  • 19
1

You can also add a space in the Text for a very simple solution:

ObjC code (added by s1m0n as comment)

[label setText:[NSString stringWithFormat:@" %@ ", text]]; 

Monotouch (C#) code:

Label.text = " "+lbltext;

@Downvoting: If you're down voting, show at least some respect by giving a reason so we can all learn why this is a bad solution. While it is certainly not a general solution for all cases, it might be a very simple solution in some cases. Because the border is created inside the Button, the text is "sticked" to the border (or there is even an overlap) and adding a space can easily fix this.

Matt
  • 1,111
  • 11
  • 20
  • I didn't down vote you - however you were probably down voted as this isn't valid objective-c. There isn't a string concatenate operator like you have in Java and whatever other languages which use a '+' to concatenate text to a string literal. Plus, objective-c string literals start with an @ sign, so it would have been @" ". In addition to this, even if this worked - the solution isn't the most practical to all cases (such as multi line text), but it would work for the simplest of cases. – bandejapaisa Jun 26 '13 at 20:23
  • I am using Monotouch (C#) for iOS development, but you are right that I should have mentioned that in my post as it is confusing in a objective-c post. – Matt Jun 27 '13 at 18:59
  • 2
    The Objective-C equivalent of this is `[label setText:[NSString stringWithFormat:@" %@ ", text]];` – s1m0n Jul 26 '13 at 08:47
  • This is such a simple solution. Kudos sir! – Dyldo42 Feb 21 '14 at 00:44
1

Alternatively, without using a label, you could use NSString method sizeWithFont:forWidth:lineBreakMode:, which returns the size of the text. Then, you could call NSString drawInRect:withFont:lineBreakMode: method, where your rect would be the one obtained from the sizeWithFont method, increased by the desired margin.

Abdullah Umer
  • 4,234
  • 5
  • 36
  • 65
Maggie
  • 7,823
  • 7
  • 45
  • 66
  • This works too, but you lose the other customization and convenience options of UILabel, such as being able to call setText and redraw, which would not be possible using Maggie's method without writing a whole custom view. – Benjamin Mayo Jul 28 '11 at 07:30