Skip to content

Instantly share code, notes, and snippets.

@minikin
minikin / getHDDSize.swift
Created January 31, 2017 08:44
Get size of hardrive on macOS
func getHDDSize() -> CGFloat {
do {
let fileAttributes = try FileManager.default.attributesOfFileSystem(forPath: "/")
if let size = fileAttributes[FileAttributeKey.systemSize] as? CGFloat {
return size
}
} catch { }
return 0
}
@minikin
minikin / loopThroughClass.m
Created February 8, 2017 08:51
Loop through all object properties at runtime: Objective-C
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
unsigned int numberOfProperties = 0;
objc_property_t *propertyArray = class_copyPropertyList([YourClass class], &numberOfProperties);
for (NSUInteger i = 0; i < numberOfProperties; i++) {
objc_property_t property = propertyArray[i];
NSString *name = [[NSString alloc] initWithUTF8String:property_getName(property)];
NSString *attributesString = [[NSString alloc] initWithUTF8String:property_getAttributes(property)];
@minikin
minikin / fonts.swift
Created February 12, 2017 22:44
Find fonts in your app
for family: String in UIFont.familyNames {
print("\(family)")
for names: String in UIFont.fontNames(forFamilyName: family) {
print("== \(names)")
}
}
@minikin
minikin / ImageExtensions.swift
Last active March 2, 2017 07:41
UIImage Extensions
extension UIImage {
func resizeImage(_ image: UIImage, newWidth: CGFloat) -> UIImage? {
let scale = newWidth / image.size.width
let newHeight = image.size.height * scale
UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
guard let image = newImage else { return nil }
UIGraphicsEndImageContext()
// MARK: - Photos
func checkIfAuthorizedToAccessPhotos(_ handler: @escaping (_ isAuthorized: Bool) -> Void) {
switch PHPhotoLibrary.authorizationStatus() {
case .notDetermined:
PHPhotoLibrary.requestAuthorization{ status in
switch status {
case .authorized:
handler(true)
default:
@minikin
minikin / Run Script
Created April 4, 2017 07:31
Automatically Generating Warnings for TODOs in Xcode
TAGS="TODO:|FIXME:"
echo "searching ${SRCROOT} for ${TAGS}"
find "${SRCROOT}" \( -name "*.swift" \) -print0 \
| xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*\$" \
| perl -p -e "s/($TAGS)/ warning: \$1/"
@minikin
minikin / RandomColorsFromString.swift
Created April 6, 2017 12:56
Random Colors From String
func randomColor(_ seed: String) -> UIColor {
var total: Int = 0
for u in seed.unicodeScalars {
total += Int(UInt32(u))
}
srand48(total * 200)
let r = CGFloat(drand48())
@minikin
minikin / randomColor.swift
Created April 6, 2017 13:56
extension UIColor
extension UIColor {
static func random() -> UIColor{
return UIColor(
red: CGFloat(drand48()),
green: CGFloat(drand48()),
blue: CGFloat(drand48()),
alpha: 1.0
)
}
}
@minikin
minikin / UICollectionViewDelegateFlowLayout.swift
Created April 7, 2017 12:18
UICollectionViewDelegateFlowLayout
let columns: CGFloat = 2.0
let inset: CGFloat = 15.0
let spacing: CGFloat = 2.0
let lineSpacing: CGFloat = 4.0
extension PinboardRootViewController : UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
@minikin
minikin / creatButton.swift
Created April 10, 2017 12:33
creatButton
func creatButton(_ cell: ChartPreviewPinCell){
let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 22, height: 22))
btn.setImage(UIImage(named: "delete_cell"), for: .normal)
btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
cell.contentView.addSubview(btn)
customButton = btn
}
func buttonAction(sender: UIButton!) {