iOS 带下划线文字

UILabel和UIButton有的时候需要添加下划线,简单的作法就是通过默认的 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 = [[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,带下划线文字)