UILabel和UIButton添加删除线和下划线
/**
* UILabel、UIButton的删除线/下划线
* mark = 0 删除线、= 1 下划线
*/
- (void)createLineInView:(UIView *)theView Mark:(NSInteger)mark {
NSString *tempStr = @"";
UIButton *tempBut;
if ([[theView class] isSubclassOfClass:[UIButton class]]) {
tempBut = (UIButton *)theView;
tempStr = tempBut.titleLabel.text;
}
UILabel *tempLab;
if ([[theView class] isSubclassOfClass:[UILabel class]]) {
tempLab = (UILabel *)theView;
tempStr = tempLab.text;
}
if (theView && ![tempStr isEqualToString:@""]) {
// 获取字符串的长度
NSUInteger length = [tempStr length];
// 设置富文本的属性
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:tempStr];
if (mark == 1) {
// 下划线
[attri addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:NSMakeRange(0, length)];
} else {
// 删除线
[attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, length)];
}
if (tempBut) {
[tempBut setAttributedTitle:attri forState:UIControlStateNormal];
}
if (tempLab) {
[tempLab setAttributedText:attri];
}
}
}