button或者lab下邊的下劃線

在開發中會遇到這樣的需求,在lab或者是button下邊需要下劃線,現在舉兩個例子


1.在button下邊加上下劃線

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 200, 200, 40);
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"我就是下劃線"];
    NSRange titleRange = {0,[title length]};
    [title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange];
    [btn setAttributedTitle:title
                      forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:20]];
    [self.view addSubview:btn];

2.在lab下邊加上下劃線

  

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 310, 350)];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    label.numberOfLines =0;
    [label setFont:[UIFont systemFontOfSize:14]];
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"喬丹  科比  詹姆斯 杜蘭特 庫裏 韋德"]];
    NSRange contentRange = {0,[content length]};
//    下劃線
    [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
//     刪除線
    [content addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(3,12)];
//     字體大小
    [content addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0] range:NSMakeRange(0, 2)];
//     字體顏色
    [content addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, 10)];
//     刪除線的顏色
    [content addAttribute:NSStrikethroughColorAttributeName value:[UIColor greenColor] range:NSMakeRange(8, 10)];
    
    label.attributedText = content;
    [self.view addSubview:label];
發佈了29 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章