Reputation: 1382
How can I set label's border which is dynamically generated (Not from Interface Builder)?
Upvotes: 11
Views: 20805
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
Reputation: 512776
Swift version
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
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
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