本文主要是介绍[iOS]UILabel和UIButton添加删除线和下划线,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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:NSUnderlineStyleAttributeNamevalue:@(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];}}
}
这篇关于[iOS]UILabel和UIButton添加删除线和下划线的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!