给UILabel和UIButton的文字加下划线

给UILabel和UIButton的文字加下划线_第1张图片



UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 100)];

label.numberOfLines = 0;

NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"同意《自动扣款协议》"]];

NSRange contentRange = {3,[content length]-4};

[content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];

label.attributedText = content;

[self.view addSubview:label];




UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];

button.frame=CGRectMake(100, 300, 300, 40);

[self.view addSubview:button];

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"同意《查看所有中奖记录》"];

NSRange strRange = {3,[str length]-4};

[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];

[str addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, 2)];

[button setAttributedTitle:str forState:UIControlStateNormal];

[button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];

你可能感兴趣的:(给UILabel和UIButton的文字加下划线)