計算寬度/高度
//方法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~