iOS_User
iOS_User

Reputation: 1382

set label border in iphone

How can I set label's border which is dynamically generated (Not from Interface Builder)?

Upvotes: 11

Views: 20805

Answers (4)

dannywartnaby
dannywartnaby

Reputation: 5542

I'm not sure you can by default with UILabel. You might want to consider using a read-only (field.editing = NO) UITextField and setting it's borderStyle (which can be done programmatically using a UITextBorderStyle). That may be a little 'heavy' though. Another option may be to sub-class UILabel to draw your border.

Alternatively, and depending on your needs this may be better, use the backing CALayer and draw a border using it's borderColor and borderWidth properties.

Upvotes: 0

Suragch
Suragch

Reputation: 512776

Swift version

enter image description here

Set label border

label.layer.borderWidth = 2.0

Set border color

label.layer.borderColor = UIColor.blueColor().CGColor

Use rounded corners

label.layer.cornerRadius = 8

Make background color stay within rounded corners

label.layer.masksToBounds = true

Upvotes: 3

Mihir Mehta
Mihir Mehta

Reputation: 13843

you can do it by

Label.layer.borderColor = [UIColor whiteColor].CGColor;
Label.layer.borderWidth = 4.0;

before this you need to import a framework QuartzCore/QuartzCore.h

Upvotes: 30

zonble
zonble

Reputation: 4213

You can also try to subclass your label and override the drawRect: method to draw or a border or whatever you like:

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] setStroke];
    CGContextStrokeRect(context, self.bounds);
}

Upvotes: 1

Related Questions