@buoge
2017-06-29T16:07:31.000000Z
字数 804
阅读 886
iOS
商品合计,活动优惠,实付,都是对齐的,后面的冒号":",是单独一个lable,实现,不然计算两端对齐一直不对
// 调用方式
justified(mLableNote)
// 核心方法
func justified(_ mLable:UILabel) {
let text = mLable.text
let textSize = text?.characters.count ?? 2
if (text == nil) {
return
}
let font = FontUtils.getThinFont(13)
let textAttributes = [NSFontAttributeName: font]
let textString = text! as NSString
let attributeLength = textSize - 1
let textRectSize = textString.boundingRect(with: CGSize(width: self.frame.size.width,height:2000), options: .usesLineFragmentOrigin, attributes: textAttributes, context: nil).size
let margin = (mLable.frame.size.width - textRectSize.width) / CGFloat(attributeLength)
let attributedString = NSMutableAttributedString(string: text ?? "")
attributedString.addAttribute(NSKernAttributeName, value: margin, range: NSRange(location: 0,length: attributeLength) )
mLable.attributedText = attributedString
}