计算宽度/高度
//方法1.已有具体 label ,给定宽度限制,计算 label 的高度
let size = label.sizeThatFits(CGSizeMake(limitW, CGFloat(MAXFLOAT)))
//方法2.已知字符串和字号(+1),通过 NSString 的 sizeWithAttributes 方法计算
let priceStr = NSString.init(format:"¥ %@",goodsModel.price_disct)
let attributes = [NSFontAttributeName:UIFont.systemFontOfSize(14)]
let size = priceStr.sizeWithAttributes(attributes)
//方法3.已知字符串,字号(+1)和宽度限制,通过 NSString 的 boundingRectWithSize 方法计算
let priceStr = NSString.init(format:"¥ %@",goodsModel.price_disct)
let attributes = [NSFontAttributeName:UIFont.systemFontOfSize(14)]
let size = priceStr.boundingRectWithSize(CGSizeMake(CGFloat(MAXFLOAT), 30), options: .UsesLineFragmentOrigin, attributes: attributes, context: nil).size
添加删除线
方法一:
let lineStr = NSAttributedString(string: oldStr,attributes: [NSStrikethroughStyleAttributeName:NSUnderlineStyle.StyleSingle.rawValue])
oldPriceLabel.attributedText = lineStr
方法二:
let attributeStr = NSMutableAttributedString(string:oldStr)
attributeStr.addAttribute(NSStrikethroughStyleAttributeName, value: NSNumber(integer: 1), range: NSMakeRange(0, attributeStr.length))
oldPriceLabel.attributedText = attributeStr
其实两个方法效果和原理都是一样的,你可以根据你的编程习惯或个人喜恶进行选择.
期待你的评论建议O(∩_∩)O~