diff --git a/Sources/dreiKit/UIImage+Manipulation.swift b/Sources/dreiKit/UIImage+Manipulation.swift new file mode 100644 index 0000000..0dae001 --- /dev/null +++ b/Sources/dreiKit/UIImage+Manipulation.swift @@ -0,0 +1,64 @@ +// +// UIImage+Manipulation.swift +// dreiKit +// +// Created by Samuel Bichsel on 05.03.21. +// + +import UIKit.UIImage + +public extension UIImage { + func withTintByMultiply(with color: UIColor) -> UIImage { + UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale); defer { UIGraphicsEndImageContext() } + guard let context = UIGraphicsGetCurrentContext(), let cgImage = cgImage else { + return self + } + + // flip the image + context.scaleBy(x: 1.0, y: -1.0) + context.translateBy(x: 0.0, y: -size.height) + + // multiply blend mode + context.setBlendMode(.multiply) + + let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) + context.clip(to: rect, mask: cgImage) + color.setFill() + context.fill(rect) + + // create UIImage + guard let newImage = UIGraphicsGetImageFromCurrentImageContext() else { + return self + } + + return newImage + } + + // https://stackoverflow.com/questions/20021478/add-transparent-space-around-a-uiimage + func withPadding(_ padding: CGFloat) -> UIImage? { + return withPadding(x: padding, y: padding) + } + + func withPadding(x: CGFloat, y: CGFloat) -> UIImage? { + let newWidth = size.width + 2 * x + let newHeight = size.height + 2 * y + let newSize = CGSize(width: newWidth, height: newHeight) + UIGraphicsBeginImageContextWithOptions(newSize, false, 0); defer { UIGraphicsEndImageContext() } + let origin = CGPoint(x: (newWidth - size.width) / 2, y: (newHeight - size.height) / 2) + draw(at: origin) + return UIGraphicsGetImageFromCurrentImageContext() + } + + func doubleHeight() -> UIImage { + let newHeight = size.height * 2 + let newSize = CGSize(width: size.width, height: newHeight) + UIGraphicsBeginImageContextWithOptions(newSize, false, 0); defer { UIGraphicsEndImageContext() } + let origin = CGPoint(x: 0, y: 0) + draw(at: origin) + guard let newImage = UIGraphicsGetImageFromCurrentImageContext() else { + return self + } + + return newImage + } +} diff --git a/Sources/dreiKit/UIView+AutoLayout.swift b/Sources/dreiKit/UIView+AutoLayout.swift index a779976..55b39ce 100644 --- a/Sources/dreiKit/UIView+AutoLayout.swift +++ b/Sources/dreiKit/UIView+AutoLayout.swift @@ -8,12 +8,23 @@ import UIKit -public extension UIView { +public struct FillConstraints { + public var leading: NSLayoutConstraint + public var trailing: NSLayoutConstraint + public var top: NSLayoutConstraint + public var bottom: NSLayoutConstraint + + public var all: [NSLayoutConstraint] { [leading, trailing, top, bottom] } +} +public extension UIView { class func autoLayout() -> Self { - let view = self.init(frame: .zero) - view.translatesAutoresizingMaskIntoConstraints = false - return view + return self.init(frame: .zero).autolayout() + } + + func autolayout() -> Self { + translatesAutoresizingMaskIntoConstraints = false + return self } func activateConstraints( @@ -34,7 +45,7 @@ public extension UIView { widthAnchor.constraint(equalTo: view.widthAnchor), topAnchor.constraint(equalTo: view.topAnchor), heightAnchor.constraint(equalTo: view.heightAnchor), - ]) + ]) } func fitVerticalScrollView() { @@ -46,31 +57,40 @@ public extension UIView { widthAnchor.constraint(equalTo: view.widthAnchor), topAnchor.constraint(equalTo: view.topAnchor), bottomAnchor.constraint(equalTo: view.bottomAnchor), - ]) + ]) } - func fillSuperview(edgeInsets: NSDirectionalEdgeInsets = .zero) { + func createFillSuperview(edgeInsets: NSDirectionalEdgeInsets = .zero) -> FillConstraints { guard let view = superview else { - return + fatalError("superview is not set") } - NSLayoutConstraint.activate([ - leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: edgeInsets.leading), - trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -edgeInsets.trailing), - topAnchor.constraint(equalTo: view.topAnchor, constant: edgeInsets.top), - bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -edgeInsets.bottom), - ]) + return FillConstraints( + leading: leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: edgeInsets.leading), + trailing: trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -edgeInsets.trailing), + top: topAnchor.constraint(equalTo: view.topAnchor, constant: edgeInsets.top), + bottom: bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -edgeInsets.bottom) + ) } - func fillSuperviewMargins(edgeInsets: NSDirectionalEdgeInsets = .zero) { + func fillSuperview(edgeInsets: NSDirectionalEdgeInsets = .zero) { + NSLayoutConstraint.activate(createFillSuperview(edgeInsets: edgeInsets)) + } + + func createFillSuperviewMargins(edgeInsets: NSDirectionalEdgeInsets = .zero) -> FillConstraints { guard let view = superview else { - return + fatalError("superview is not set") } - NSLayoutConstraint.activate([ - leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor, constant: edgeInsets.leading), - trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor, constant: -edgeInsets.trailing), - topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: edgeInsets.top), - bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -edgeInsets.bottom), - ]) + + return FillConstraints( + leading: leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor, constant: edgeInsets.leading), + trailing: trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor, constant: -edgeInsets.trailing), + top: topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: edgeInsets.top), + bottom: bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -edgeInsets.bottom) + ) + } + + func fillSuperviewMargins(edgeInsets: NSDirectionalEdgeInsets = .zero) { + NSLayoutConstraint.activate(createFillSuperviewMargins(edgeInsets: edgeInsets)) } func fillWithLowTrailingPriority() { @@ -106,7 +126,12 @@ public extension UIView { var layoutHeight: CGFloat { return systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height } +} +public extension NSLayoutConstraint { + static func activate(_ constraints: FillConstraints) { + activate(constraints.all) + } } public protocol ViewInit: class { diff --git a/dreiKit.podspec b/dreiKit.podspec index b247c45..c208c0f 100644 --- a/dreiKit.podspec +++ b/dreiKit.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "dreiKit" - s.version = "1.0.4" + s.version = "1.0.5" s.summary = "A short description of dreiKit." s.homepage = "https://github.com/dreipol/dreiKit" s.license = { type: 'MIT', file: 'LICENSE' }