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

移动开发 iOS
本文为各位51CTO的网友朋友们介绍了iOS开发实战中如何自定义带下划线文本的UIButton,文章比较简洁,直接将Uiunderlinedbutton.h和Uiunderlinedbutton.m的代码整理出来,各位读者可以亲自实现一边这些代码,在慢慢消化了解。

Uiunderlinedbutton.h代码

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

Uiunderlinedbutton.m代码

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

 

责任编辑:闫佳明 来源: apkbus
相关推荐

2023-11-29 18:11:17

Python代码

2021-08-08 22:27:13

Python下划线方法

2021-12-26 22:55:26

Python下划线编程

2010-03-04 10:35:51

Python下划线

2011-08-02 11:17:13

iOS开发 View

2020-09-22 09:41:48

Python下划线开发

2021-07-31 19:21:34

Python下划线数值

2023-11-09 08:55:17

Python双下划线

2013-06-27 11:10:01

iOS开发自定义UISlider

2014-05-08 10:36:59

CSS单词连字符

2009-06-08 20:13:36

Eclipse自定义控

2013-07-18 16:09:10

自定义iOS状态栏iOS开发iOS学习

2021-01-20 08:58:39

iOS 14桌面图标快捷指令

2013-05-30 15:53:17

iOS开发iOS SDKPopver

2020-10-24 11:09:35

Python下划线用法

2011-12-26 10:11:08

JSP

2009-06-24 15:13:36

自定义JSF组件

2017-10-25 14:07:54

APPiOSxcode

2012-06-01 11:02:33

2012-12-24 14:42:48

iOS自定义状态栏
点赞
收藏

51CTO技术栈公众号