iOS开发小技巧 - UILabel添加中划线

时间:2022-06-01 21:11:59

iOS开发小技巧

遇到的问题: 给Label添加中划线,然后并没有效果

NSString *str = [NSString stringWithFormat:@"合计金额 ¥%.2f",model.amount];
NSDictionary *attrDic = @{
NSStrikethroughStyleAttributeName: @(1),
NSFontAttributeName : Font(FONT_SIZE_12)
};
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str attributes:attrDic];
self.priceLabel.attributedText = attrStr;
  • 自我赶脚代码是完美的,但是就是不出效果,捉急!

解决方案 : 增加一个富文本属性 NSBaselineOffsetAttributeName : @(NSUnderlineStyleSingle)

NSString *str = [NSString stringWithFormat:@"合计金额 ¥%.2f",model.amount];
NSDictionary *attrDic = @{
NSStrikethroughStyleAttributeName: @(1),
NSFontAttributeName : Font(FONT_SIZE_12),
NSBaselineOffsetAttributeName : @(NSUnderlineStyleSingle)
};
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str attributes:attrDic];
self.priceLabel.attributedText = attrStr;

iOS开发小技巧 - UILabel添加中划线