0

Possible Duplicate:
CALayer: add a border only at one side

I need to draw a left border of an UILabel programmatically. Using this code it will be drawn all borders (top, left, bottom, right) around the label.

myLabel.layer.borderColor = [UIColor whiteColor].CGColor;
myLabel.layer.borderWidth = 1.5f;

Is there a chance to draw only left-border using QuartzCore or I need to add a subview (e.g. a UIView with small weight and background) to my UILabel?

Community
  • 1
  • 1

1 Answers1

2

This is pretty easy. I have done some similar programming to add "transparent" shapes (that show the background color through a button) on a view. The 15 minute method is to:

  1. create a UILabel of the appropriate size (CGRectMake(0,0,1.5f,myLabel.frame.size.height)) and color
  2. add the label to your view (this will clip the UIlabel with cornerRadius, etc)
  3. set the autoresizingMask to UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight to let the left border resize with myLabel and stick to the left side

I think that's about it. Should work with very little drama. If you are drawing hundreds or thousands it might be slow but for a few labels, no problem.

Enjoy,

Damien

El Developer
  • 3,345
  • 1
  • 21
  • 40
Damien Del Russo
  • 1,040
  • 8
  • 19
  • Thank you! That's an interesting solution, but following the linked question I think it's easier create a CALayer then add it as a sublayer. It's easier also if you need to "extend" UILabel class with a category (e.g. UILabel+Border). Thanks for your reply again! – Giorgio Marziani de Paolis Feb 27 '12 at 22:48
  • 1
    I agree, the linked solution offsetting the frame is more clever and will scale better. This is one thing I like about trying to answer questions - I usually learn something as well. – Damien Del Russo Feb 28 '12 at 03:23