diff --git a/Examples/ImagesExample-macOS/Sources/ImagesExample-macOS/main.swift b/Examples/ImagesExample-macOS/Sources/ImagesExample-macOS/main.swift index 2fbca8b..a6114ac 100644 --- a/Examples/ImagesExample-macOS/Sources/ImagesExample-macOS/main.swift +++ b/Examples/ImagesExample-macOS/Sources/ImagesExample-macOS/main.swift @@ -8,15 +8,11 @@ import PlaceholderKit import Cocoa let images = [ - PlaceholderBuilder().coloredBackground(color: .red, - size: CGSize(width: 100, height: 100)), - PlaceholderBuilder().coloredBackground(color: .blue, - size: CGSize(width: 100, height: 200)), - PlaceholderBuilder().coloredBackground(color: .green, - size: CGSize(width: 200, height: 100)), - PlaceholderBuilder().coloredBackground(color: .yellow, - size: CGSize(width: 320, height: 480)) -].compactMap({$0}) + Placeholder(size: CGSize(width: 100, height: 100), backgroundStyle: .solidColor(.red)), + Placeholder(size: CGSize(width: 200, height: 100), backgroundStyle: .solidColor(.blue)), + Placeholder(size: CGSize(width: 100, height: 200), backgroundStyle: .solidColor(.green)), + Placeholder(size: CGSize(width: 320, height: 480), backgroundStyle: .solidColor(.yellow)), +].compactMap({$0.render()}) let desktopURL = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first! diff --git a/Examples/ImagesExample/ImagesExample/ViewController.swift b/Examples/ImagesExample/ImagesExample/ViewController.swift index cc066be..506d457 100644 --- a/Examples/ImagesExample/ImagesExample/ViewController.swift +++ b/Examples/ImagesExample/ImagesExample/ViewController.swift @@ -13,11 +13,11 @@ class ViewController: UIViewController { var sampleImagesCollectionViewController: SampleImagesCollectionViewController? lazy var images: [UIImage] = { return [ - PlaceholderBuilder().coloredBackground(color: .red, size: CGSize(width: 100, height: 100)), - PlaceholderBuilder().coloredBackground(color: .blue, size: CGSize(width: 200, height: 100)), - PlaceholderBuilder().coloredBackground(color: .green, size: CGSize(width: 100, height: 200)), - PlaceholderBuilder().coloredBackground(color: .yellow, size: CGSize(width: 320, height: 480)), - ].compactMap({$0}) + Placeholder(size: CGSize(width: 100, height: 100), backgroundStyle: .solidColor(.red)), + Placeholder(size: CGSize(width: 200, height: 100), backgroundStyle: .solidColor(.blue)), + Placeholder(size: CGSize(width: 100, height: 200), backgroundStyle: .solidColor(.green)), + Placeholder(size: CGSize(width: 320, height: 480), backgroundStyle: .solidColor(.yellow)), + ].compactMap({$0.render()}) }() override func viewDidLoad() { diff --git a/Sources/PlaceholderKit/NSImage+Placeholder.swift b/Sources/PlaceholderKit/NSImage+Placeholder.swift index 95c7804..5f7768a 100644 --- a/Sources/PlaceholderKit/NSImage+Placeholder.swift +++ b/Sources/PlaceholderKit/NSImage+Placeholder.swift @@ -9,26 +9,26 @@ #if canImport(Cocoa) import Cocoa -extension NSImage { - static func image(withColor color: NSColor, size: CGSize) -> NSImage? { +extension Placeholder { + public func coloredBackground() -> Image? { let image = NSImage(size: size) let rect = CGRect(origin: .zero, size: size) image.lockFocus() // background - color.drawSwatch(in: rect) - - // text - let text = PlaceholderBuilder().displayedText(size: size) - let attrs = PlaceholderBuilder().displayedTextAttributes() - let textRect = PlaceholderBuilder().textRect(imageSize: size) + switch backgroundStyle { + case .solidColor(let color): + color.drawSwatch(in: rect) + default: + break + } // draw text - text.draw(in: textRect, withAttributes: attrs) + displayedText.draw(in: textRect, + withAttributes: displayedTextAttributes) image.unlockFocus() return image } } - #endif diff --git a/Sources/PlaceholderKit/PlaceholderKit.swift b/Sources/PlaceholderKit/PlaceholderKit.swift index 6f92b11..0505204 100644 --- a/Sources/PlaceholderKit/PlaceholderKit.swift +++ b/Sources/PlaceholderKit/PlaceholderKit.swift @@ -26,45 +26,35 @@ public struct Placeholder { self.size = size self.backgroundStyle = backgroundStyle } -} - -public struct PlaceholderBuilder { - public func coloredBackground(color: Color, size: CGSize) -> Image? { - return Image.image(withColor: color, size: size) - } - - public func imageBackground(image: Image, size: CGSize, tiled: Bool = false) -> Image? { - return nil - } // not platform specific - public func displayedTextAttributes() -> [NSAttributedString.Key: NSObject] { + public var displayedTextAttributes: [NSAttributedString.Key: NSObject] { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center - let attrs = [NSAttributedString.Key.font: Font(name: "HelveticaNeue", size: 10)!, NSAttributedString.Key.paragraphStyle: paragraphStyle] + let attrs = [NSAttributedString.Key.font: Font(name: "HelveticaNeue", size: 20)!, NSAttributedString.Key.paragraphStyle: paragraphStyle] return attrs } - public func displayedText(size: CGSize) -> String { - let string = "\(Int(size.width))x\(Int(size.height))" - return string + public var displayedText: String { + return "\(Int(size.width))x\(Int(size.height))" } - public func textRect(imageSize: CGSize) -> CGRect { - let attributedString = NSAttributedString(string: displayedText(size: imageSize), - attributes: displayedTextAttributes()) + public var textRect: CGRect { + let attributedString = NSAttributedString(string: displayedText, + attributes: displayedTextAttributes) let textSize = attributedString.size() let rect = CGRect(x: 0, - y: (imageSize.height - textSize.height) / 2, - width: imageSize.width, - height: textSize.height) + y: (size.height - textSize.height) / 2, + width: size.width, + height: size.height) return rect - } - public init() {} + public func render() -> Image? { + return coloredBackground() + } } public enum BackgroundStyle { diff --git a/Sources/PlaceholderKit/UIImage+Placeholder.swift b/Sources/PlaceholderKit/UIImage+Placeholder.swift index 65ca6a2..9f90613 100644 --- a/Sources/PlaceholderKit/UIImage+Placeholder.swift +++ b/Sources/PlaceholderKit/UIImage+Placeholder.swift @@ -9,27 +9,30 @@ #if canImport(UIKit) import UIKit -extension UIImage { - static func image(withColor color: UIColor, size: CGSize) -> UIImage? { +extension Placeholder { + public func coloredBackground() -> Image? { let renderer = UIGraphicsImageRenderer(size: size) let image = renderer.image { context in let rect = CGRect(origin: .zero, size: size) - // background - color.setFill() - context.fill(rect) - // text - let text = PlaceholderBuilder().displayedText(size: size) - let attrs = PlaceholderBuilder().displayedTextAttributes() - let textRect = PlaceholderBuilder().textRect(imageSize: size) + // background + switch backgroundStyle { + case .solidColor(let color): + color.setFill() + context.fill(rect) + default: + break + } // draw text - text.draw(with: textRect, options: .usesLineFragmentOrigin, attributes: attrs, context: nil) + displayedText.draw(with: textRect, + options: .usesLineFragmentOrigin, + attributes: displayedTextAttributes, + context: nil) } return image } - } #endif