Skip to content

Instantly share code, notes, and snippets.

@xslim
Created October 21, 2019 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xslim/9dd51bb54bf714749ecf5fbbf6d312ac to your computer and use it in GitHub Desktop.
Save xslim/9dd51bb54bf714749ecf5fbbf6d312ac to your computer and use it in GitHub Desktop.
//
// TransactionReceiptView.swift
// AdyenPOSTerminal
//
// Created by Taras Kalapun on 11/19/15.
//
//
import UIKit
import AdyenToolkit
@objc class TransactionReceiptView: UIView {
@objc var logoImage: UIImage?
@objc var transactionData: ADYTransactionData?
@objc var receipt: ADYReceipt? {
didSet {
self.redraw()
}
}
let receiptWidth:CGFloat = 300//250;
let fontSize:CGFloat = 12.0
let paddingHor:CGFloat = 18
let paddingVert:CGFloat = 36
let sectionSpacing:CGFloat = 18
var normalFont: UIFont!
var boldFont: UIFont!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
override func layoutSubviews() {
super.layoutSubviews()
}
func redraw() {
self.frame.size = calculateSize()
self.setNeedsDisplay()
//self.setNeedsLayout()
self.invalidateIntrinsicContentSize()
self.superview?.layoutIfNeeded()
}
func calculateSize() -> CGSize {
return calculateAndDraw(false)
}
func calculateAndDraw(_ draw: Bool = true) -> CGSize {
var offset: CGFloat = paddingVert + 10 //metrics.scaledLogoSize.height
if let lines = (receipt?.header as NSArray?) as? [ADYReceiptLine] {
offset = drawLines(lines, offset: offset, draw: draw)
}
if let lines = (receipt?.content as NSArray?) as? [ADYReceiptLine] {
offset = drawLines(lines, offset: offset, draw: draw)
}
if let lines = (receipt?.footer as NSArray?) as? [ADYReceiptLine] {
offset = drawLines(lines, offset: offset, draw: draw)
}
return CGSize(width: receiptWidth, height: offset)
}
override func draw(_ rect: CGRect) {
// Drawing code
_ = calculateAndDraw(true);
}
override var intrinsicContentSize : CGSize {
let size = self.frame.size;
return size;
}
func drawLines(_ lines:[ADYReceiptLine], offset: CGFloat, draw: Bool = true) -> CGFloat {
var offset = offset
offset += sectionSpacing
for line in lines {
let lineHeight = renderLine(line, atOffset: offset, draw: draw)
offset += lineHeight
}
return offset
}
func commonInit() {
normalFont = UIFont.systemFont(ofSize: fontSize)
boldFont = UIFont.boldSystemFont(ofSize: fontSize)
self.backgroundColor = UIColor.white //ColorCompatibility.secondarySystemBackground //UIColor.white
// self.layer.borderColor = Color.adyColor(, nil)ine).CGColor
// self.layer.borderWidth = 1
// self.autoresizesSubviews = true
// self.autoresizingMask = [.FlexibleWidth, .FlexibleBottomMargin]
//
// guard let tx = transactionData else {
// return
// }
}
func stringAlignRight(_ string: String, totalLength: Int, pad: Character) -> String {
var string = string
let amountToPad = totalLength - string.count
if amountToPad < 1 {
return string
}
let padString = String(pad)
for _ in 1...amountToPad {
string = padString + string
}
return string
}
func renderLine(_ line: ADYReceiptLine, atOffset offset: CGFloat, draw: Bool = true) -> CGFloat {
let font: UIFont = line.isEmphasized ? boldFont : normalFont
let drawAttr: [NSAttributedString.Key : AnyObject] = [NSAttributedString.Key.font: font]
let dashSize: CGFloat = ("-" as NSString).size(withAttributes: drawAttr).width
switch line.type {
case .image:
let image: UIImage = line.image
var scale: CGFloat = 0.6
if image.size.width * scale > receiptWidth {
scale = receiptWidth / image.size.width
}
let receiptWidthAfterPadding: CGFloat = receiptWidth - (paddingHor * 2)
let x: CGFloat = paddingHor + ((receiptWidthAfterPadding - (image.size.width * scale)) / 2)
if draw {
image.draw(in: CGRect(x: x, y: offset, width: image.size.width * scale, height: image.size.height * scale))
}
return image.size.height * scale
case .nameValue:
(line.name as NSString).draw(at: CGPoint(x: paddingHor, y: offset), withAttributes: drawAttr)
let valueWidth: CGFloat = (line.value as NSString).size(withAttributes: drawAttr).width
if draw {
line.value.draw(at: CGPoint(x: receiptWidth - paddingHor - valueWidth, y: offset), withAttributes: drawAttr)
}
return normalFont.lineHeight
case .name:
if line.name.hasPrefix("---") && line.name.count == 3 {
line.name = stringAlignRight("-", totalLength: Int( receiptWidth / dashSize), pad: "-")
line.name.draw(at: CGPoint(x: 0, y: offset), withAttributes: drawAttr)
} else {
let style: NSMutableParagraphStyle = NSMutableParagraphStyle()
style.lineBreakMode = .byWordWrapping
style.alignment = .center
let drawAttrX: [NSAttributedString.Key : AnyObject] = [NSAttributedString.Key.font: font, NSAttributedString.Key.paragraphStyle: style]
var rect: CGRect = (line.name as NSString).boundingRect(with: CGSize(width: receiptWidth, height: CGFloat.greatestFiniteMagnitude), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: drawAttrX, context: nil)
rect.origin.y = offset
rect.size.width = receiptWidth
if draw {
line.name.draw(in: rect, withAttributes: drawAttrX)
}
}
return normalFont.lineHeight
case .empty:
return normalFont.lineHeight
@unknown default:
return normalFont.lineHeight
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment