如何给Label中的文字划线。
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 100, 30)];
[self.view addSubview:lable];
lable.backgroundColor = [UIColor redColor];
lable.textAlignment = NSTextAlignmentCenter;
NSString *oldPrice = @"¥ 999999";
NSUInteger length = [oldPrice length];
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:oldPrice];
[attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, length)]; //添加下划线
[attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, length)];//设置下划线线的颜色
[lable setAttributedText:attri];
记录下来,希望日后自己可以用到。
其中NSMutableAttributedString类来自Foundation框架,而文字相关的其他属性在UIKit/NSAttributedString.
效果图如下所示: