给UILabel和UIButton的文字加下划线



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];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容