您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)IOS開(kāi)發(fā)之UILabel或者UIButton加下劃線鏈接的示例分析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
IOS 開(kāi)發(fā)之UILabel 或者 UIButton加下劃線鏈接
本文主要介紹了IOS中 UILable及UIButton的帶下劃線鏈接的實(shí)現(xiàn)方法及附有源碼下載,大家開(kāi)發(fā)IOS 應(yīng)用有需要的可以參考下:
方法一:
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"查看所有中獎(jiǎng)記錄"]; 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
直接將這個(gè)類(lèi) copy 到工程中,,然后將需要加下劃線的 Button 類(lèi)名改為 HyperlinksButton就可以了,提供了 setColor: 這個(gè)接口,可以設(shè)置下劃線顏色,代碼很簡(jiǎn)單,不解釋了。UILabel 同理可得。
示例結(jié)果:
感謝各位的閱讀!關(guān)于“IOS開(kāi)發(fā)之UILabel或者UIButton加下劃線鏈接的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。