Skip to content

Instantly share code, notes, and snippets.

@KelvinJin
Last active February 9, 2017 07:03
Show Gist options
  • Save KelvinJin/9072bce5acefbb115065d7a1202de7eb to your computer and use it in GitHub Desktop.
Save KelvinJin/9072bce5acefbb115065d7a1202de7eb to your computer and use it in GitHub Desktop.
import UIKit
class SimpleCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var titleLabel: UILabel!
func bind(color: String) {
titleLabel.text = "\(arc4random_uniform(1000))"
backgroundColor = .clear
contentView.backgroundColor = color.hexColor
}
}
extension String {
var hexColor: UIColor {
let hex = trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
var int = UInt32()
Scanner(string: hex).scanHexInt32(&int)
let a, r, g, b: UInt32
switch hex.characters.count {
case 3: // RGB (12-bit)
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
case 6: // RGB (24-bit)
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
case 8: // ARGB (32-bit)
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
default:
return .clear
}
return UIColor(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
}
}
class ViewController: UICollectionViewController {
let cellIdentifier = "SimpleCollectionViewCell"
let vcs = ["f44336", "9c27b0", "3f51b5", "03a9f4", "009688", "8bc34a"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Must turn paging on.
class SimpleCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var titleLabel: UILabel!
func bind(color: String) {
titleLabel.text = "\(arc4random_uniform(1000))"
backgroundColor = .clear
contentView.backgroundColor = color.hexColor
}
}
extension String {
var hexColor: UIColor {
let hex = trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
var int = UInt32()
Scanner(string: hex).scanHexInt32(&int)
let a, r, g, b: UInt32
switch hex.characters.count {
case 3: // RGB (12-bit)
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
case 6: // RGB (24-bit)
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
case 8: // ARGB (32-bit)
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
default:
return .clear
}
return UIColor(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
}
}
class ViewController: UICollectionViewController {
let cellIdentifier = "SimpleCollectionViewCell"
let vcs = ["f44336", "9c27b0", "3f51b5", "03a9f4", "009688", "8bc34a"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Must turn paging on.
collectionView?.isPagingEnabled = true
}
}
extension ViewController: UICollectionViewDelegateFlowLayout {
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return vcs.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let c = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath)
if let cell = c as? SimpleCollectionViewCell {
let v = vcs[indexPath.row]
cell.bind(color: v)
}
return c
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return view.bounds.size
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return .zero
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment