ios 按钮下面加下划线_IOS 开发之UILabel 或者 UIButton加下划线链接

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

@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

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

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值