Nav apraksta

UILabelExt.swift 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // UILabelExt.swift
  3. // ExtensionKit
  4. //
  5. // Created by FFIB on 2017/9/14.
  6. // Copyright © 2017年 FFIB. All rights reserved.
  7. //
  8. import UIKit
  9. extension UILabel {
  10. //Calcute the single line height according to the font
  11. public var singleHeight: CGFloat {
  12. if let attributedString = attributedText {
  13. return attributedString.size().height
  14. }else {
  15. return 0
  16. }
  17. }
  18. //Adjust the size according to the designed size
  19. public func sizeToFit(with designedSize: CGSize) {
  20. calcuteSize(designedWidth: designedSize.width,
  21. designedHeight: designedSize.height)
  22. }
  23. //Adjust the size according to he required number of rows
  24. public func sizeToFitWithLine(with designedWidth: CGFloat) {
  25. calcuteSize(designedWidth: designedWidth,
  26. designedHeight: singleHeight * CGFloat(numberOfLines))
  27. }
  28. private func calcuteSize(designedWidth: CGFloat,
  29. designedHeight: CGFloat) {
  30. guard let attributedString = attributedText else {
  31. return
  32. }
  33. let fitSize = attributedString.boundingRect(with: CGSize(width: designedWidth, height: 10000),
  34. options: .usesLineFragmentOrigin,
  35. context: nil)
  36. let finalHeight = min(designedHeight, fitSize.height)
  37. let fitConstraint = [NSLayoutConstraint(item: self,
  38. attribute: .width,
  39. relatedBy: .equal,
  40. toItem: nil,
  41. attribute: .width,
  42. multiplier: 1,
  43. constant: designedWidth),
  44. NSLayoutConstraint(item: self,
  45. attribute: .height,
  46. relatedBy: .equal,
  47. toItem: nil,
  48. attribute: .height,
  49. multiplier: 1,
  50. constant: finalHeight)]
  51. NSLayoutConstraint.activate(fitConstraint)
  52. }
  53. //if using the storyboard and the nib ,need to call withint the method 'viewDidLayoutSubviews'
  54. public func setLineSpace(space: CGFloat) {
  55. guard let str = text else {
  56. return
  57. }
  58. let paragraphStyle = NSMutableParagraphStyle()
  59. paragraphStyle.lineSpacing = space
  60. let attributedString = NSMutableAttributedString(string: str)
  61. attributedString.addAttributes([NSAttributedStringKey.paragraphStyle: paragraphStyle],
  62. range: NSRange(location: 0, length: str.characters.count))
  63. attributedString.addAttributes([NSAttributedStringKey.font: font],
  64. range: NSRange(location: 0, length: str.characters.count))
  65. attributedText = attributedString
  66. }
  67. public func setWordSpace(space: CGFloat) {
  68. guard let str = text else {
  69. return
  70. }
  71. let attributedString = NSMutableAttributedString(string: str,
  72. attributes: [NSAttributedStringKey.paragraphStyle : NSMutableParagraphStyle(),
  73. NSAttributedStringKey.kern: space])
  74. attributedString.addAttributes([NSAttributedStringKey.font: font],
  75. range: NSRange(location: 0, length: str.characters.count))
  76. attributedText = attributedString
  77. }
  78. public func setSpace(lineSpace: CGFloat,
  79. wordSpace: CGFloat) {
  80. guard let str = text else {
  81. return
  82. }
  83. let paragraphStyle = NSMutableParagraphStyle()
  84. paragraphStyle.lineSpacing = lineSpace
  85. let attributedString = NSMutableAttributedString(string: str,
  86. attributes: [NSAttributedStringKey.kern: wordSpace])
  87. attributedString.addAttributes([NSAttributedStringKey.paragraphStyle: paragraphStyle],
  88. range: NSRange(location: 0, length: str.characters.count))
  89. attributedString.addAttributes([NSAttributedStringKey.font: font],
  90. range: NSRange(location: 0, length: str.characters.count))
  91. attributedText = attributedString
  92. }
  93. }