当前位置: 移动技术网 > IT编程>移动开发>IOS > IOS 开发之UILabel 或者 UIButton加下划线链接

IOS 开发之UILabel 或者 UIButton加下划线链接

2019年07月24日  | 移动技术网IT编程  | 我要评论

安娜·塞米诺维奇,美或再次测试萨德,山有墓兮墓有龙

ios 开发之uilabel 或者 uibutton加下划线链接

         本文主要介绍了ios中 uilable及uibutton的带下划线链接的实现方法及附有源码下载,大家开发ios 应用有需要的可以参考下:

方法一:

nsmutableattributedstring *str = [[nsmutableattributedstring alloc] initwithstring:@"查看所有中奖记录"]; 
nsrange strrange = {0,[str length]}; 
[str addattribute:nsunderlinestyleattributename value:[nsnumber numberwithinteger:nsunderlinestylesingle] range:strrange]; 
[_awarddisplaybtn setattributedtitle:str forstate:uicontrolstatenormal]; 

方法二:

hyperlinksbutton.h

#import <uikit/uikit.h> 
 
@interface hyperlinksbutton : uibutton 
{ 
  uicolor *linecolor; 
} 
-(void)setcolor:(uicolor*)color; 
@end 

hyperlinksbutton.m 
[objc] view plain copy print?
#import "hyperlinksbutton.h" 
 
@implementation hyperlinksbutton 
 
- (id)initwithframe:(cgrect)frame 
{ 
  self = [super initwithframe:frame]; 
  if (self) { 
     
  } 
  return self; 
} 
 
-(void)setcolor:(uicolor *)color{ 
  linecolor = [color copy]; 
  [self setneedsdisplay]; 
} 
 
 
- (void) drawrect:(cgrect)rect { 
  cgrect textrect = self.titlelabel.frame; 
  cgcontextref contextref = uigraphicsgetcurrentcontext(); 
   
  cgfloat descender = self.titlelabel.font.descender; 
  if([linecolor iskindofclass:[uicolor class]]){ 
    cgcontextsetstrokecolorwithcolor(contextref, linecolor.cgcolor); 
  } 
   
  cgcontextmovetopoint(contextref, textrect.origin.x, textrect.origin.y + textrect.size.height + descender+1); 
  cgcontextaddlinetopoint(contextref, textrect.origin.x + textrect.size.width, textrect.origin.y + textrect.size.height + descender+1); 
   
  cgcontextclosepath(contextref); 
  cgcontextdrawpath(contextref, kcgpathstroke); 
} 
@end 

直接将这个类 copy 到工程中,,然后将需要加下划线的 button 类名改为 hyperlinksbutton就可以了,提供了 setcolor: 这个接口,可以设置下划线颜色,代码很简单,不解释了。uilabel 同理可得。

示例结果:

源码下载地址:https://github.com/chaoyuan899/hyperlinksbutton#how-to-use

本站源码下载:

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网