Skip to content

Instantly share code, notes, and snippets.

@jakelazaroff
Last active January 9, 2018 00:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jakelazaroff/68b5cea60809f52a0cee to your computer and use it in GitHub Desktop.
Save jakelazaroff/68b5cea60809f52a0cee to your computer and use it in GitHub Desktop.
A simple class that draws a PaintCode StyleKit "Image Method" image centered inside a UIView
// SKImage
// A simple class that draws a PaintCode StyleKit "Image Method" image centered inside a UIView
// - installation: add to project and replace STYLEKIT_NAME comment with name of StyleKit class
// - instantiation: SKImage(image: "NameOfImage")
// - instantiation (with custom size): SKImage(image: "NameOfImage", size: CGRectMake(0, 0, 16, 16))
import UIKit
class SKImage: UIView {
typealias StyleKit = /* STYLEKIT_NAME */
let view = UIImageView()
var size: CGRect?
convenience init(image: String, size: CGRect? = nil) {
// fetch the UIImage from StyleKit
let image = StyleKit.valueForKey("imageOf" + image) as! UIImage
// by default, use the size passed in or the dimensions of the image
let _size = size ?? CGRect(
x: 0,
y: 0,
width: image.size.width,
height: image.size.height
)
self.init(frame: _size)
self.size = _size
// scale the image as the imageview scales
view.contentMode = .ScaleAspectFill
view.image = image
centerIcon()
addSubview(view)
}
// center the image in the view
func centerIcon() {
view.frame = CGRect(
x: (bounds.size.width / 2) - (size!.width / 2),
y: (bounds.size.height / 2) - (size!.height / 2),
width: size!.width,
height: size!.height
)
}
// recenter image if the view changes dimensions
override func layoutSubviews() {
centerIcon()
}
}
@VprGTS
Copy link

VprGTS commented Aug 28, 2016

How do I call this for my View Controller? Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment