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
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
希望与广大网友互动??
点此进行留言吧!