码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发-UILabel和UIButton添加下划线

时间:2015-06-17 09:23:28      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选择第一种,第二种有兴趣的可以自己研究一下。

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 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"博文地址:http://www.cnblogs.com/xiaofeixiang 博客园-FlyElephant QQ群:228407086"]];
    NSRange contentRange = {0,[content length]};
    [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
    
    label.attributedText = content;
    [self.view addSubview:label];

UIButton设置下划线:

  UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(10, 200, 80, 30)];
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"FlyElephant"];
    NSRange titleRange = {0,[title length]};
    [title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange];
    [button setAttributedTitle:title
                      forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor redColor]];
    [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.view addSubview:button];

最终效果如下:

技术分享

iOS开发-UILabel和UIButton添加下划线

标签:

原文地址:http://www.cnblogs.com/xiaofeixiang/p/4582292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!