Skip to content

Commit

Permalink
Fixed column width calculation
Browse files Browse the repository at this point in the history
Use actual width of strings when system font is applied instead of assuming 14 pixels per character
  • Loading branch information
ndreisg committed Mar 11, 2021
1 parent 993d966 commit 9e33bde
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion SwiftDataTables/Classes/Cells/DataCell/DataCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UIKit
class DataCell: UICollectionViewCell {

//MARK: - Properties
private enum Properties {
public enum Properties {
static let verticalMargin: CGFloat = 5
static let horizontalMargin: CGFloat = 15
static let widthConstant: CGFloat = 20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ extension UIScrollView {
self.contentOffset = CGPoint(x: -contentInset.left, y: -contentInset.top)
}
}

extension String {
func widthOfString(usingFont font: UIFont) -> CGFloat {
let fontAttributes = [NSAttributedString.Key.font: font]
let size = self.size(withAttributes: fontAttributes)
return size.width
}
}
2 changes: 1 addition & 1 deletion SwiftDataTables/Classes/Models/DataStructureModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public struct DataStructureModel {
for column in Array(0..<self.headerTitles.count) {
let averageForCurrentColumn = Array(0..<data.count).reduce(0){
let dataType: DataTableValueType = data[$1][column]
return $0 + dataType.stringRepresentation.count
return $0 + Int(dataType.stringRepresentation.widthOfString(usingFont: UIFont.systemFont(ofSize: 17)).rounded(.up))
}
columnContentAverages.append((data.count == 0) ? 1 : Float(averageForCurrentColumn) / Float(data.count))
}
Expand Down
11 changes: 3 additions & 8 deletions SwiftDataTables/Classes/SwiftDataTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,8 @@ extension SwiftDataTable {
/// - Returns: The automatic width of the column irrespective of the Data Grid frame width
func automaticWidthForColumn(index: Int) -> CGFloat {
let columnAverage: CGFloat = CGFloat(dataStructure.averageDataLengthForColumn(index: index))
let sortingArrowVisualElementWidth: CGFloat = 20 // This is ugly
let averageDataColumnWidth: CGFloat = columnAverage * self.pixelsPerCharacter() + sortingArrowVisualElementWidth
let sortingArrowVisualElementWidth: CGFloat = 10 // This is ugly
let averageDataColumnWidth: CGFloat = columnAverage + sortingArrowVisualElementWidth + (DataCell.Properties.horizontalMargin * 2)
return max(averageDataColumnWidth, max(self.minimumColumnWidth(), self.minimumHeaderColumnWidth(index: index)))
}

Expand All @@ -701,12 +701,7 @@ extension SwiftDataTable {
}

func minimumHeaderColumnWidth(index: Int) -> CGFloat {
return CGFloat(self.pixelsPerCharacter() * CGFloat(self.dataStructure.headerTitles[index].count))
}

//There should be an automated way to retrieve the font size of the cell
func pixelsPerCharacter() -> CGFloat {
return 14
return CGFloat(self.dataStructure.headerTitles[index].widthOfString(usingFont: UIFont.boldSystemFont(ofSize: 17)))
}

func heightForPaginationView() -> CGFloat {
Expand Down

0 comments on commit 9e33bde

Please sign in to comment.