nRewik / SimplePDF

Create a simple PDF effortlessly. :smile:
MIT License
253 stars 53 forks source link

Alignments don't work in a table #22

Closed ozitrance closed 6 years ago

ozitrance commented 6 years ago

Hi there and thank you for this great library.

I noticed that alignments don't work when creating tables. It is always aligned to the left. Trying center and right has no effect.

Just following the example from the main page:

let tableDef = TableDefinition(alignments: [.center, .right],
                                        columnWidths: [200, 200],
                                        fonts: [UIFont.systemFont(ofSize: 16), UIFont.systemFont(ofSize: 16)],
                                        textColors: [UIColor.black, UIColor.blue])

        let data = [["test1","test1"]]

        pdf.addTable(data.count,
                     columnCount: 2,
                     rowHeight: 25,
                     tableLineWidth: 0, // this is taken from the definition
            tableDefinition: tableDef,
            dataArray: data)

I'm using 4.5.1 from cocoapods. Any ideas?

nico75005 commented 6 years ago

It's not used at all. You can replace the content of the method line 302 in SimplePDF.swift

fileprivate func drawTable(rowCount: Int, alignment: ContentAlignment, columnCount: Int, rowHeight: CGFloat, columnWidth: CGFloat?, tableLineWidth: CGFloat, font: UIFont?, tableDefinition:TableDefinition?, dataArray: Array<Array<String>>, currentOffset: CGPoint) -> CGRect

with this:

fileprivate func drawTable(rowCount: Int, alignment: ContentAlignment, columnCount: Int, rowHeight: CGFloat, columnWidth: CGFloat?, tableLineWidth: CGFloat, font: UIFont?, tableDefinition:TableDefinition?, dataArray: Array<Array<String>>, currentOffset: CGPoint) -> CGRect {

        let height = (CGFloat(rowCount)*rowHeight)

        let drawRect = CGRect(x: currentOffset.x, y: currentOffset.y, width: pageBounds.width - pageMarginLeft - pageMarginRight, height: height)

        UIColor.black.setStroke()
        UIColor.black.setFill()

        let tableWidth = { () -> CGFloat in
            if let cws = tableDefinition?.columnWidths {
                return cws.reduce(0, { (result, current) -> CGFloat in
                    return result + current
                })
            } else if let cw = columnWidth {
                return CGFloat(columnCount) * cw
            }

            return 0 // default which should never be use, because either columnWidth, or columnsWidths is set
        }()

        for i in 0...rowCount {
            let newOrigin = drawRect.origin.y + rowHeight*CGFloat(i)

            let from = CGPoint(x: drawRect.origin.x, y: newOrigin)
            let to = CGPoint(x: drawRect.origin.x + tableWidth, y: newOrigin)

            drawLineFromPoint(from, to: to, lineWidth: tableLineWidth)
        }

        for i in 0...columnCount {
            let currentOffset = { () -> CGFloat in
                if let cws = tableDefinition?.columnWidths {
                    var offset:CGFloat = 0
                    for x in 0..<i {
                        offset += cws[x]
                    }
                    return offset
                } else if let cw = columnWidth {
                    return cw * CGFloat(i)
                }

                return 0 // default which should never be use, because either columnWidth, or columnsWidths is set
            }()

            let newOrigin = drawRect.origin.x + currentOffset

            let from = CGPoint(x: newOrigin, y: drawRect.origin.y)
            let to = CGPoint(x: newOrigin, y: drawRect.origin.y + CGFloat(rowCount)*rowHeight)

            drawLineFromPoint(from, to: to, lineWidth: tableLineWidth)
        }

        for i in 0..<rowCount {
            for j in 0...columnCount-1 {
                let currentOffset = { () -> CGFloat in
                    if let cws = tableDefinition?.columnWidths {
                        var offset:CGFloat = 0
                        for x in 0..<j {
                            offset += cws[x]
                        }
                        return offset
                    } else if let cw = columnWidth {
                        return cw * CGFloat(j)
                    }

                    return 0 // default which should never be use, because either columnWidth, or columnsWidths is set
                }()

                let newOriginX = drawRect.origin.x + currentOffset
                let newOriginY = drawRect.origin.y + ((CGFloat(i)*rowHeight))

                let currentAlignment = { () -> ContentAlignment in
                    if let a = tableDefinition?.alignments {
                        if (a.count > j){
                            return a[j]
                        }
                    } else {
                        return alignment
                    }

                    return .left
                }()

                let currentFont = { () -> UIFont in
                    if let f = tableDefinition?.fonts {
                        if (f.count > j){
                            return f[j]
                        }
                    } else if let f = font {
                        return f
                    }

                    return UIFont.systemFont(ofSize: UIFont.systemFontSize)
                }()

                let currentTextColor = { () -> UIColor in
                    if let t = tableDefinition?.textColors {
                        if t.count > j {
                            return t[j]
                        }
                    }

                    return UIColor.black
                }()

                let currentColumnWidth = { () -> CGFloat in
                    if let cw = tableDefinition?.columnWidths {
                        if cw.count > j {
                            return cw[j]
                        }
                    } else if let cw = columnWidth {
                        return cw
                    }

                    return 100 // default which should never be use, because either columnWidth, or columnsWidths is set
                }()

                let frame = CGRect(x: newOriginX, y: newOriginY, width: currentColumnWidth, height: rowHeight)
                drawTextInCell(frame, text: dataArray[i][j] as NSString, alignment: currentAlignment, font: currentFont, textColor: currentTextColor)
            }
        }

        return drawRect
    }
ozitrance commented 6 years ago

@nico75005 It worked perfectly ... 👍 Thank you so much!!!