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阅读 9,119评论 2 7
  • 以下为主要代码 #import@interface FirstViewController : UIViewCon...
    十年一品温如言1008阅读 3,978评论 0 0
  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 4,276评论 0 0
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 7,311评论 1 14
  • 被某人带动,突然也想试试在简书写点什么……虽然我一直号称有写作障碍。 从三月正式学习占星后,个人觉得有...
    月蝎女sandy阅读 1,573评论 0 2