Skip to content

Instantly share code, notes, and snippets.

@chaitanyaSoni96
Created June 14, 2019 11:06
Show Gist options
  • Save chaitanyaSoni96/b686821feb81730a99df75162e299abf to your computer and use it in GitHub Desktop.
Save chaitanyaSoni96/b686821feb81730a99df75162e299abf to your computer and use it in GitHub Desktop.
CSKoladaCardView
//
// CSKoladaView.swift
// SellQwik
//
// Created by Chaitanya Soni on 16/04/19.
//
import Foundation
class CSKolodaCardView{
private var imageViewForProduct:UIImageView!
private var labelForProductName:UILabel!
private var labelForProductPrice:UILabel!
func setView(catProdItem:CatalogCategoryAssignedProductsData, viewToCustomize:UIView) -> UIView {
let customizedView = customizeView(viewToCustomize: viewToCustomize)
imageViewForProduct.contentMode = .scaleAspectFit
imageViewForProduct.moa.url = catProdItem.image
labelForProductName.text = catProdItem.name
labelForProductPrice.text = "Rs. "+catProdItem.price
customizedView.addSubview(imageViewForProduct)
customizedView.addSubview(labelForProductName)
customizedView.addSubview(labelForProductPrice)
return customizedView
}
func customizeView(viewToCustomize:UIView) -> UIView{
viewToCustomize.layer.masksToBounds = false
viewToCustomize.layer.shadowOffset = CGSize(width: 0,height: 0)
viewToCustomize.layer.shadowRadius = 10
viewToCustomize.layer.shadowOpacity = 0.3
viewToCustomize.backgroundColor = .white
viewToCustomize.layer.cornerRadius = 5.0
imageViewForProduct = UIImageView()
labelForProductName = UILabel()
labelForProductPrice = UILabel()
imageViewForProduct.frame = CGRect(x: 0, y: 0, width: viewToCustomize.frame.size.width, height: viewToCustomize.frame.size.height*0.85)
labelForProductName.frame = CGRect(x: 0, y: imageViewForProduct.frame.size.height, width: viewToCustomize.frame.size.width/2, height: viewToCustomize.frame.size.height*0.1)
labelForProductPrice.frame = CGRect(x: labelForProductName.frame.width, y: imageViewForProduct.frame.size.height, width: viewToCustomize.frame.size.width/2, height: viewToCustomize.frame.size.height*0.1)
let scanPink = hexStringToUIColor(hex: "#DB3263")
labelForProductName.textColor = scanPink; labelForProductPrice.textColor = scanPink;
labelForProductPrice.textAlignment = .center; labelForProductName.textAlignment = .center;
return viewToCustomize
}
func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if ((cString.count) != 6) {
return UIColor.gray
}
var rgbValue:UInt32 = 0
Scanner(string: cString).scanHexInt32(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
}
extension UIColor {
convenience init(red: Int, green: Int, blue: Int) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
assert(blue >= 0 && blue <= 255, "Invalid blue component")
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
}
convenience init(rgb: Int) {
self.init(
red: (rgb >> 16) & 0xFF,
green: (rgb >> 8) & 0xFF,
blue: rgb & 0xFF
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment