首页 > 移动平台 > 详细

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

时间:2014-11-20 06:41:55      阅读:384      评论:0      收藏:0      [点我收藏+]

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 

 

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

原文:http://www.cnblogs.com/lgphp/p/4109635.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!