- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
if (_shouldUnderline) {
CGFloat size = self.width;
// CGSize size =[self.text sizeWithFont:self.font
// forWidth:self.frame.size.width
// lineBreakMode:NSLineBreakByWordWrapping];
CGSize allSize = [self.text sizeWithFont:self.font];
double currentNumOfLines = ceil(allSize.width/size);
CGFloat startX = 0;
switch (self.textAlignment) {
case UITextAlignmentLeft:
startX = rect.origin.x;
break;
case UITextAlignmentCenter:
startX = (rect.size.width - allSize.width)/2.0f;
break;
case UITextAlignmentRight:
startX = rect.size.width - allSize.width;
break;
default:
break;
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, self.textColor.CGColor);
CGContextBeginPath(context);
for (int i=1; i<=currentNumOfLines; i++) {
if(i == currentNumOfLines)
{
CGFloat halfWayUp = rect.size.height-1 + rect.origin.y;
CGContextMoveToPoint(context, startX, halfWayUp);
CGContextAddLineToPoint(context, startX + allSize.width - size*(currentNumOfLines-1) +2, halfWayUp);
}else{
CGFloat halfWayUp = rect.size.height*i/currentNumOfLines - 1 + rect.origin.y;
CGContextMoveToPoint(context, startX, halfWayUp);
CGContextAddLineToPoint(context, startX + size +2, halfWayUp);
}
}
CGContextStrokePath(context);
}
}
- (void)setTextToConfigFram:(NSString *)text
{
[super setText:text];
CGFloat fHeight = [LayoutHelper heightForLabel:self WithText:text];
[self setNumberOfLines:0];
[self setFrame:CGRectMake(self.originX, self.originY, self.size.width, fHeight)];
}
- (void)addTarget:(id)target action:(SEL)action
{
[self setUserInteractionEnabled:TRUE];
_actionView = [[UIControl alloc] initWithFrame:self.bounds];
[_actionView setBackgroundColor:[UIColor clearColor]];
[_actionView addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}
- (void)appendHighlightedColor
{
self.backgroundColor = self.highlightedColor;
}
- (void)removeHighlightedColor
{
self.backgroundColor = [UIColor clearColor];
}
@end