[关闭]
@buoge 2017-06-29T16:07:31.000000Z 字数 804 阅读 893

UILable 两端对齐

iOS


image_1bjma4r9g1g6t154f9417id7919.png-6kB

商品合计,活动优惠,实付,都是对齐的,后面的冒号":",是单独一个lable,实现,不然计算两端对齐一直不对

  1. // 调用方式
  2. justified(mLableNote)
  3. // 核心方法
  4. func justified(_ mLable:UILabel) {
  5. let text = mLable.text
  6. let textSize = text?.characters.count ?? 2
  7. if (text == nil) {
  8. return
  9. }
  10. let font = FontUtils.getThinFont(13)
  11. let textAttributes = [NSFontAttributeName: font]
  12. let textString = text! as NSString
  13. let attributeLength = textSize - 1
  14. let textRectSize = textString.boundingRect(with: CGSize(width: self.frame.size.width,height:2000), options: .usesLineFragmentOrigin, attributes: textAttributes, context: nil).size
  15. let margin = (mLable.frame.size.width - textRectSize.width) / CGFloat(attributeLength)
  16. let attributedString = NSMutableAttributedString(string: text ?? "")
  17. attributedString.addAttribute(NSKernAttributeName, value: margin, range: NSRange(location: 0,length: attributeLength) )
  18. mLable.attributedText = attributedString
  19. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注