diff --git a/GLTapLabelDemo/GLTapLabel.h b/GLTapLabelDemo/GLTapLabel.h index 6938c7f..0a2ce01 100644 --- a/GLTapLabelDemo/GLTapLabel.h +++ b/GLTapLabelDemo/GLTapLabel.h @@ -34,5 +34,6 @@ @property(nonatomic,assign) id delegate; @property(nonatomic,retain) UIColor *linkColor; +@property(nonatomic,assign) BOOL underlineLink; @end diff --git a/GLTapLabelDemo/GLTapLabel.m b/GLTapLabelDemo/GLTapLabel.m index 47358dd..984887e 100644 --- a/GLTapLabelDemo/GLTapLabel.m +++ b/GLTapLabelDemo/GLTapLabel.m @@ -26,9 +26,11 @@ @implementation GLTapLabel @synthesize delegate; @synthesize linkColor; +@synthesize underlineLink; -(void)drawTextInRect:(CGRect)rect { + CGContextRef context = UIGraphicsGetCurrentContext(); UIColor *origColor = [self textColor]; [origColor set]; if(!hotFont){ @@ -47,6 +49,8 @@ -(void)drawTextInRect:(CGRect)rect __block CGPoint drawPoint = CGPointMake(0,0); NSString *read; NSScanner *s = [NSScanner scannerWithString:self.text]; + [s setCharactersToBeSkipped: nil]; + while ([s scanUpToCharactersFromSet:[NSCharacterSet symbolCharacterSet] intoString:&read]) { NSArray *origWords = [read componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; NSMutableArray *words = [NSMutableArray array]; @@ -74,6 +78,8 @@ -(void)drawTextInRect:(CGRect)rect [words addObject:origWord]; } + CGContextSetStrokeColorWithColor( context, self.linkColor.CGColor ); + CGContextSetLineWidth( context, 1.0 ); [words enumerateObjectsUsingBlock:^(NSString *word, NSUInteger idx, BOOL *stop) { BOOL hot = [word hasPrefix:@"#"] || [word hasPrefix:@"@"]; UIFont *f= hot ? hotFont : self.font; @@ -85,6 +91,11 @@ -(void)drawTextInRect:(CGRect)rect [hotZones addObject:[NSValue valueWithCGRect:CGRectMake(drawPoint.x, drawPoint.y, s.width, s.height)]]; [hotWords addObject:word]; [linkColor set]; + if ( self.underlineLink ) + { + CGContextMoveToPoint( context, drawPoint.x, drawPoint.y+s.height ); + CGContextAddLineToPoint( context, drawPoint.x+s.width, drawPoint.y+s.height ); + } } [word drawAtPoint:drawPoint withFont:f]; [origColor set]; @@ -95,7 +106,8 @@ -(void)drawTextInRect:(CGRect)rect while ([s scanCharactersFromSet:[NSCharacterSet symbolCharacterSet] intoString:&read]) { for(int idx=0;idx rect.size.width) { drawPoint = CGPointMake(0, drawPoint.y + s.height); @@ -105,6 +117,8 @@ -(void)drawTextInRect:(CGRect)rect } } } + if ( self.underlineLink ) + CGContextStrokePath( context ); } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event