//
//  UILabelWrap.m
//  UILabelWrap
//
//  Created by Shaun Harrison on 2/13/09.
//  Copyright 2009 enormego. All rights reserved.
//

#import "UILabelWrap.h"

@implementation UILabelWrap
@synthesize text, font, color, highlightedTextColor, textColor=color, shadowColor, shadowOffset;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}

- (void)setText:(NSString*)newText {
	if([newText isEqualToString:text]) return;
	
	if(text) [text release];
	text = [newText copy];
	[self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
	if(!self.text) return;
	if(!isHighlighted && self.shadowColor) {
		[self.shadowColor set];
		CGRect offsetRect = rect;
		offsetRect.origin = CGPointMake(offsetRect.origin.x+self.shadowOffset.width, offsetRect.origin.y+self.shadowOffset.height);
		[self.text drawInRect:offsetRect withFont:self.font lineBreakMode:UILineBreakModeWordWrap|UILineBreakModeTailTruncation alignment:UITextAlignmentLeft];
	}
	
	if(isHighlighted) {
		[self.highlightedTextColor set];
	} else {
		[self.color set];
	}
	[self.text drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeWordWrap|UILineBreakModeTailTruncation alignment:UITextAlignmentLeft];
}

- (UIFont*)font {
	if(!font) {
		self.font = [UIFont systemFontOfSize:17.0f];
	}
	
	return font;
}

- (UIColor*)color {
	if(!color) {
		self.color = [UIColor blackColor];
	}
	
	return color;
}

- (UIColor*)highlightedTextColor {
	if(!highlightedTextColor) {
		self.highlightedTextColor = [UIColor whiteColor];
	}
	
	return highlightedTextColor;
}

- (void)setHighlighted:(BOOL)highlighted {
	isHighlighted = highlighted;
	[self setNeedsDisplay];
}


- (BOOL)isHighlighted {
	return isHighlighted;
}

- (void)setFrame:(CGRect)frame {
	[super setFrame:frame];
	[self setNeedsDisplay];
}

- (void)dealloc {
	if(text) [text release];
	if(font) [font release];
	if(color) [color release];
	if(highlightedTextColor) [highlightedTextColor release];
	if(shadowColor) [shadowColor release];
	[super dealloc];
}


@end
