Last active
January 9, 2018 00:42
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do I call this for my View Controller? Thank you very much.