1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
/// 获取标准格式的金额 /// - Parameters: /// - str: 金额 /// - fontSize: 字体大小 func getAttributStrFormatMoney(str: String, fontSize: CGFloat , sizeOffSet:CGFloat?=4 , fontColor:UIColor?=UIColor.red) -> NSAttributedString{ let attributeStr = NSMutableAttributedString(string: str) //let r = Range(0..<3) //old: let _ = NSRange(location: 0, length: 3) let nsstr = str as NSString // 查找金额 let signalRange = nsstr.range(of: "¥") if signalRange.length > 0 { let signalNumRange = NSRange(location: signalRange.location, length: attributeStr.length - signalRange.location) // 设置为字体颜色 attributeStr.setAttributes([NSForegroundColorAttributeName: fontColor!], range: signalNumRange) // 查找 小数点 let pointRange = nsstr.range(of: ".") // 设置 字体大小 let numFontSize = fontSize + sizeOffSet! if pointRange.length > 0 { let numRange = NSRange(location: signalRange.location + signalRange.length, length: pointRange.location + pointRange.length - (signalRange.location + signalRange.length)) // 设置 字体大小 attributeStr.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: numFontSize), range: numRange) }else{ let numRange = NSRange(location: signalRange.location + signalRange.length, length: attributeStr.length - (signalRange.location + signalRange.length) ) // 设置 字体大小 attributeStr.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: numFontSize), range: numRange) } } return attributeStr }
|
近期评论