Redesigned API

This commit is contained in:
Simon 2018-12-27 15:48:53 +08:00
parent 80bb42fa79
commit fc8ecca3fb
5 changed files with 47 additions and 58 deletions

View File

@ -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!

View File

@ -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() {

View File

@ -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

View File

@ -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 {

View File

@ -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