Skip to content

Commit

Permalink
fix: add numberOflines
Browse files Browse the repository at this point in the history
  • Loading branch information
vcellu committed Jul 5, 2022
1 parent 3ad9472 commit d77beeb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
1 change: 1 addition & 0 deletions ios/CellContentStyle.swift
Expand Up @@ -12,4 +12,5 @@ struct CellContentStyle: Decodable {
var color: String?
var fontFamily: String?
var fontSize: Int?
var rowHeight: Int?
}
5 changes: 3 additions & 2 deletions ios/DataCellView.swift
Expand Up @@ -13,6 +13,7 @@ class DataCellView: UICollectionViewCell {
var borderColor = UIColor.black.withAlphaComponent(0.1)
var selectionsEngine: SelectionsEngine?
var cellColor: UIColor?
var numberOfLines = 1;
static let minWidth: CGFloat = 40

override init(frame: CGRect) {
Expand All @@ -34,7 +35,7 @@ class DataCellView: UICollectionViewCell {
let col = cols[index]

if let label = views[index] as? PaddedLabel {
let newFrame = CGRect(x: x, y: 0, width: Int(col.width!), height: theme.rowHeight!)
let newFrame = CGRect(x: x, y: 0, width: Int(col.width!), height: theme.rowHeight! * numberOfLines)
label.textAlignment = element.qNum == nil ? .left : .right
x += Int(col.width!)
label.frame = newFrame.integral
Expand All @@ -44,7 +45,7 @@ class DataCellView: UICollectionViewCell {
label.cell = element
label.checkSelected(selectionsEngine)
label.textColor = cellColor!

label.numberOfLines = numberOfLines
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion ios/DataCollectionView.swift
Expand Up @@ -23,12 +23,14 @@ class DataCollectionView: UIView, UICollectionViewDataSource, UICollectionViewDe
var selectionsEngine: SelectionsEngine?
let reuseIdentifier = "CellIdentifier"
var cellColor = UIColor.black
var cellStyle = CellContentStyle()

init(frame: CGRect, withRows rows: [DataRow], andColumns cols: [DataColumn], theme: TableTheme, selectionsEngine: SelectionsEngine, cellStyle: CellContentStyle) {
super.init(frame: frame)
self.tableTheme = theme
self.selectionsEngine = selectionsEngine
let colorParser = ColorParser()
self.cellStyle = cellStyle
if let colorString = cellStyle.color {
cellColor = colorParser.fromCSS(cssString: colorString)
}
Expand Down Expand Up @@ -129,6 +131,7 @@ class DataCollectionView: UIView, UICollectionViewDataSource, UICollectionViewDe

cell.backgroundColor = indexPath.row % 2 == 0 ? .white : UIColor(red: 0.97, green: 0.97, blue: 0.97, alpha: 1.0)
cell.cellColor = cellColor
cell.numberOfLines = cellStyle.rowHeight ?? 1
if let data = dataRows {
let dataRow = data[indexPath.row]
cell.selectionsEngine = self.selectionsEngine
Expand All @@ -143,7 +146,8 @@ class DataCollectionView: UIView, UICollectionViewDataSource, UICollectionViewDe

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = dataColumns?.reduce(0, {$0 + $1.width!}) ?? frame.width
return CGSize(width: width, height: CGFloat(tableTheme!.rowHeight!))
let height = cellStyle.rowHeight ?? 1
return CGSize(width: width, height: CGFloat(tableTheme!.rowHeight! * height))
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
Expand Down
33 changes: 19 additions & 14 deletions ios/PaddedLabel.swift
Expand Up @@ -26,26 +26,31 @@ class PaddedLabel: UILabel, SelectionsListener {
fatalError("init(coder:) has not been implemented")
}

override var intrinsicContentSize: CGSize {
numberOfLines = 0 // don't forget!
var s = super.intrinsicContentSize
s.height += UIEI.top + UIEI.bottom
s.width += UIEI.left + UIEI.right
return s
}
// override var intrinsicContentSize: CGSize {
// numberOfLines = 0 // don't forget!
// var s = super.intrinsicContentSize
// s.height += UIEI.top + UIEI.bottom
// s.width += UIEI.left + UIEI.right
// return s
// }

override func drawText(in rect: CGRect) {
let r = rect.inset(by: UIEI)
super.drawText(in: r)
if(numberOfLines != 1) {
let r = self.textRect(forBounds: rect.inset(by: UIEI), limitedToNumberOfLines: self.numberOfLines)
super.drawText(in: r)
} else {
let r = rect.inset(by: UIEI)
super.drawText(in: r)
}
}

override func textRect(forBounds bounds: CGRect,
limitedToNumberOfLines n: Int) -> CGRect {
let b = bounds
let tr = b.inset(by: UIEI)
let ctr = super.textRect(forBounds: tr, limitedToNumberOfLines: 0)
// that line of code MUST be LAST in this function, NOT first
return ctr

let ctr = super.textRect(forBounds: bounds, limitedToNumberOfLines: n)
let xOffset = self.textAlignment == .left ? PaddedLabel.PaddingSize : -PaddedLabel.PaddingSize
return CGRect(x: ctr.origin.x + CGFloat(xOffset), y: ctr.origin.y + 8, width: ctr.size.width, height: ctr.size.height)

}

func makeSelectable(selectionsEngine: SelectionsEngine) {
Expand Down

0 comments on commit d77beeb

Please sign in to comment.