Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. public class HorizontalScrollGrid: UIScrollView {
  2.  
  3. public var arrTitles: [String] = []
  4.  
  5. public var itemHeight: CGFloat = 45.0
  6.  
  7. public var verticalSpacing: CGFloat = 10.0
  8. public var horizontalSpacing: CGFloat = 10.0
  9.  
  10. public var numberOfRows: Int = 4
  11.  
  12. public var textFont: UIFont = UIFont.systemFont(ofSize: 15.0, weight: .bold)
  13. public var textForeground: UIColor = UIColor.black
  14. public var textBackground: UIColor = UIColor.yellow
  15. public var textAlignment: NSTextAlignment = NSTextAlignment.center
  16. public var textPadding: CGFloat = 20.0
  17.  
  18. public var roundedCorners: Bool = true
  19.  
  20. public var borderColor: UIColor = UIColor.black
  21. public var borderWidth: CGFloat = 1.0
  22.  
  23. public override init(frame: CGRect) {
  24. super.init(frame: frame)
  25. setup()
  26. }
  27.  
  28. required init?(coder aDecoder: NSCoder) {
  29. fatalError("init(coder:) has not been implemented")
  30. }
  31.  
  32. public func setup() {
  33.  
  34. var maxX: CGFloat = 0.0
  35. var maxY: CGFloat = 0.0
  36.  
  37. var totalWidth: CGFloat = 0.0
  38. var totalHeight: CGFloat = 0.0
  39.  
  40. let itemsPerRow = (arrTitles.count/numberOfRows)
  41.  
  42. for (index, text) in arrTitles.enumerated() {
  43.  
  44. let button = UIButton(type: .custom)
  45. button.setTitle(text, for: .normal)
  46. button.setTitleColor(textForeground, for: .normal)
  47. button.titleLabel?.font = textFont
  48. button.backgroundColor = textBackground
  49. button.sizeToFit()
  50.  
  51. var width: CGFloat = 60.0
  52. var height: CGFloat = 0.0
  53.  
  54. let currentRow = index/itemsPerRow
  55.  
  56. if index >= (currentRow + 1) {
  57. height = (CGFloat((currentRow)) * verticalSpacing) + (CGFloat((currentRow)) * itemHeight)
  58. if currentRow != 0 && (index/currentRow) == itemsPerRow && (index % currentRow) == 0 {
  59. totalWidth = 0.0
  60. }
  61. } else {
  62. height = 0
  63. if index == 0 {
  64. totalWidth = 0.0
  65. }
  66. }
  67.  
  68. if button.frame.width + textPadding > 60.0 {
  69. width = button.frame.width + textPadding
  70. }
  71.  
  72. let xCoord = CGFloat(totalWidth + horizontalSpacing)
  73.  
  74. button.frame = CGRect(x: xCoord, y: height, width: width, height: itemHeight)
  75.  
  76. if roundedCorners {
  77. button.layer.cornerRadius = button.frame.height / 2
  78. }
  79. button.layer.borderColor = borderColor.cgColor
  80. button.layer.borderWidth = borderWidth
  81. button.layer.masksToBounds = true
  82.  
  83. totalWidth = button.frame.maxX + horizontalSpacing
  84. totalHeight = button.frame.maxY
  85.  
  86. if totalWidth > maxX {
  87. maxX = totalWidth
  88. }
  89.  
  90. if (totalHeight + (2 * verticalSpacing) + itemHeight) > maxY {
  91. maxY = (totalHeight + (2 * verticalSpacing) + itemHeight)
  92. }
  93.  
  94. self.addSubview(button)
  95. }
  96.  
  97. self.contentSize = CGSize(width: maxX, height: maxY)
  98.  
  99. }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement