iOS开发:自定义带下划线文本的UIButton

Uiunderlinedbutton.h代码

  1. @interface UIUnderlinedButton : UIButton { 
  2. } 
  3. + (UIUnderlinedButton *) underlinedButton; 
  4. @end 

Uiunderlinedbutton.m代码

  1. @implementation UIUnderlinedButton 
  2.  
  3. + (UIUnderlinedButton*) underlinedButton { 
  4. UIUnderlinedButton* button = [[UIUnderlinedButton alloc] init]; 
  5. return [button autorelease]; 
  6. } 
  7.  
  8. - (void) drawRect:(CGRect)rect { 
  9. CGRect textRect = self.titleLabel.frame; 
  10.  
  11. // need to put the line at top of descenders (negative value) 
  12. CGFloat descender = self.titleLabel.font.descender; 
  13.  
  14. CGContextRef contextRef = UIGraphicsGetCurrentContext(); 
  15.  
  16. // set to same colour as text 
  17. CGContextSetStrokeColorWithColor(contextRef, self.titleLabel.textColor.CGColor); 
  18.  
  19. CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender); 
  20.  
  21. CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender); 
  22.  
  23. CGContextClosePath(contextRef); 
  24. CGContextDrawPath(contextRef, kCGPathStroke); 
  25. } 
  26. @end 

 

posted on 2014-11-20 01:32  lgphp  阅读(430)  评论(0)    收藏  举报