0

I'm new to subclassing, but I wanted a UILabel subclass to give any text within the label to have a 3-pixel outline. From this page, I used this method:

- (void)drawTextInRect:(CGRect)rect 
{    
    CGSize shadowOffset = self.shadowOffset;   
    UIColor *textColor = self.textColor;    

    CGContextRef c = UIGraphicsGetCurrentContext();   

    CGContextSetLineWidth(c, 3);   
    CGContextSetLineJoin(c, kCGLineJoinRound);    
    CGContextSetTextDrawingMode(c, kCGTextStroke);   
    self.textColor = [UIColor whiteColor];   
    [super drawTextInRect:rect];    

    CGContextSetTextDrawingMode(c, kCGTextFill);   
    self.textColor = textColor;   

    self.shadowOffset = CGSizeMake(0, 0);   
    [super drawTextInRect:rect];    self.shadowOffset = shadowOffset;  
} 

This works great, and I can change the colors to display any color I'd like for both the text and the outline.

Can someone let me know how to create a property called "outlineColor" that will allow me to set this subclass to any label I want and change the color of the outline?

Essentially, I'd like to be able to set a label's class to "CustomLabelClass" and then within some other class I'd like to say something like:

[myLabel setOutlineColor:[UIColor whiteColor]];

I'm not sure how to go about this. Thanks.

Community
  • 1
  • 1
achiral
  • 601
  • 2
  • 11
  • 24

1 Answers1

1

I did the same thing in my code. I created a subclass of UILabel with properties to set the border color and border width.

JKBorderedLabel.h

@interface JKBorderedLabel : UILabel

@property (nonatomic, retain) UIColor *borderColor;
@property (nonatomic) NSInteger borderWidth;

@end

JKBorderedLabel.m

#import "JKBorderedLabel.h"

@implementation JKBorderedLabel

@synthesize borderColor = _borderColor;
@synthesize borderWidth = _borderWidth;

- (void)drawTextInRect:(CGRect)rect {

    CGSize shadowOffset = self.shadowOffset;
    UIColor *textColor = self.textColor;

    self.shadowOffset = CGSizeMake(0, 0);

    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(c, _borderWidth);
    CGContextSetLineJoin(c, kCGLineJoinRound);

    CGContextSetTextDrawingMode(c, kCGTextStroke);
    self.textColor = _borderColor;
    [super drawTextInRect:rect];

    CGContextSetTextDrawingMode(c, kCGTextFill);
    self.textColor = textColor;
    [super drawTextInRect:rect];

    self.shadowOffset = shadowOffset;
}

- (void)sizeToFit
{
    [super sizeToFit];

    self.frame = CGRectMake(self.frame.origin.x,
                               self.frame.origin.y - _borderWidth,
                               self.frame.size.width + (_borderWidth * 2),
                               self.frame.size.height);
}

@end

And then to use:

JKBorderedLabel *myLabel = [[JKBorderedLabel alloc] init];

myLabel.text = @"Hello World";
myLabel.textColor = [UIColor whiteColor];
myLabel.borderColor = [UIColor blueColor];
myLabel.borderWidth = 4;
[myLabel sizeToFit];
jonkroll
  • 15,682
  • 4
  • 50
  • 43
  • Thanks! I thought I had tried something similar to this, but in other classes whenever I typed "[myLabel " the option for "setOutlineColor" never popped up. I did, however, set the class in Interface Builder, rather than initializing it via code. Could that have been a problem, do you think? – achiral Feb 08 '12 at 02:19
  • It's possible. I haven't tried using this through IB, but assuming the class of the object is set to your custom class in the Identity Inspector I would think it should work. – jonkroll Feb 08 '12 at 02:34