NSMutableAttributedString設置,下劃線

UILabel設置下劃線

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 310, 50)];

label.backgroundColor = [UIColor redColor];

[label setLineBreakMode:NSLineBreakByWordWrapping];

label.numberOfLines =3;

[label setFont:[UIFont systemFontOfSize:14]];

NSMutableAttributedString*content = [[NSMutableAttributedStringalloc]initWithString:[NSStringstringWithFormat:@"博文地址:http://www.cnblogs.com/xiaofeixiang博客園-FlyElephant QQ群:228407086"]];

NSRangecontentRange = {0,[content length]};

[content addAttribute:NSUnderlineStyleAttributeNamevalue:[NSNumbernumberWithInteger:NSUnderlineStyleSingle] range:contentRange];

label.attributedText = content;

[self.view addSubview:label];

UIButton設置下劃線:

UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(10, 200, 80, 30)];

NSMutableAttributedString*title = [[NSMutableAttributedStringalloc] initWithString:@"FlyElephant"];

NSRangetitleRange = {0,[title length]};

[title addAttribute:NSUnderlineStyleAttributeNamevalue:[NSNumbernumberWithInteger:NSUnderlineStyleSingle] range:titleRange];

[button setAttributedTitle:title

forState:UIControlStateNormal];

[button setBackgroundColor:[UIColor redColor]];

[button.titleLabel setFont:[UIFont systemFontOfSize:14]];

[self.view addSubview:button];

方法二:

HyperlinksButton.h

[objc]view plaincopyprint?

#import?

@interfaceHyperlinksButton?:?UIButton

{

UIColor*lineColor;

}

-(void)setColor:(UIColor*)color;

@end

HyperlinksButton.m

[objc]view plaincopyprint?

#import?"HyperlinksButton.h"

@implementationHyperlinksButton

-?(id)initWithFrame:(CGRect)frame

{

self=?[superinitWithFrame:frame];

if(self)?{

}

returnself;

}

-(void)setColor:(UIColor*)color{

lineColor?=?[colorcopy];

[selfsetNeedsDisplay];

}

-?(void)drawRect:(CGRect)rect?{

CGRect?textRect?=self.titleLabel.frame;

CGContextRef?contextRef?=?UIGraphicsGetCurrentContext();

CGFloat?descender?=self.titleLabel.font.descender;

if([lineColorisKindOfClass:[UIColorclass]]){

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • iOS開發系列--網絡開發 概覽 大部分應用程序都或多或少會牽扯到網絡開發,例如說新浪微博、微信等,這些應用本身可...
    lichengjin閱讀 3,721評論 2 7
  • 以下為主要代碼 #import@interface FirstViewController : UIViewCon...
    十年一品溫如言1008閱讀 697評論 0 0
  • //設置尺寸為屏幕尺寸的時候self.window = [[UIWindow alloc] initWithFra...
    LuckTime閱讀 838評論 0 0
  • *7月8日上午 N:Block :跟一個函數塊差不多,會對里面所有的內容的引用計數+1,想要解決就用__block...
    炙冰閱讀 2,547評論 1 14
  • 被某人帶動,突然也想試試在簡書寫點什么……雖然我一直號稱有寫作障礙。 從三月正式學習占星后,個人覺得有...
    月蝎女sandy閱讀 238評論 0 2