Render text rect on macOS

This commit is contained in:
Simon 2018-12-26 23:36:46 +08:00
parent 1a9df20313
commit 80bb42fa79
3 changed files with 47 additions and 27 deletions

View File

@ -6,14 +6,26 @@
// Copyright © 2018 Shao Ping Lee. All rights reserved. // Copyright © 2018 Shao Ping Lee. All rights reserved.
// //
#if canImport(AppKit) #if canImport(Cocoa)
import AppKit import Cocoa
extension NSImage { extension NSImage {
static func image(withColor color: NSColor, size: CGSize) -> NSImage? { static func image(withColor color: NSColor, size: CGSize) -> NSImage? {
let image = NSImage(size: size) let image = NSImage(size: size)
let rect = CGRect(origin: .zero, size: size)
image.lockFocus() image.lockFocus()
color.drawSwatch(in: NSMakeRect(0, 0, size.width, size.height))
// background
color.drawSwatch(in: rect)
// text
let text = PlaceholderBuilder().displayedText(size: size)
let attrs = PlaceholderBuilder().displayedTextAttributes()
let textRect = PlaceholderBuilder().textRect(imageSize: size)
// draw text
text.draw(in: textRect, withAttributes: attrs)
image.unlockFocus() image.unlockFocus()
return image return image
} }

View File

@ -10,10 +10,12 @@
import AppKit import AppKit
public typealias Color = NSColor public typealias Color = NSColor
public typealias Image = NSImage public typealias Image = NSImage
public typealias Font = NSFont
#else #else
import UIKit import UIKit
public typealias Color = UIColor public typealias Color = UIColor
public typealias Image = UIImage public typealias Image = UIImage
public typealias Font = UIFont
#endif #endif
public struct Placeholder { public struct Placeholder {
@ -35,24 +37,34 @@ public struct PlaceholderBuilder {
return nil return nil
} }
public init() {} // not platform specific
} public func displayedTextAttributes() -> [NSAttributedString.Key: NSObject] {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
public struct PlaceholderSettings { let attrs = [NSAttributedString.Key.font: Font(name: "HelveticaNeue", size: 10)!, NSAttributedString.Key.paragraphStyle: paragraphStyle]
let size: CGSize
let background: BackgroundStyle
let showDimensionAsText: Bool
let convertDimensionToAspectRatio: Bool
public init(size: CGSize, return attrs
background: BackgroundStyle,
showDimensionAsText: Bool,
convertDimensionToAspectRatio: Bool) {
self.size = size
self.background = background
self.showDimensionAsText = showDimensionAsText
self.convertDimensionToAspectRatio = convertDimensionToAspectRatio
} }
public func displayedText(size: CGSize) -> String {
let string = "\(Int(size.width))x\(Int(size.height))"
return string
}
public func textRect(imageSize: CGSize) -> CGRect {
let attributedString = NSAttributedString(string: displayedText(size: imageSize),
attributes: displayedTextAttributes())
let textSize = attributedString.size()
let rect = CGRect(x: 0,
y: (imageSize.height - textSize.height) / 2,
width: imageSize.width,
height: textSize.height)
return rect
}
public init() {}
} }
public enum BackgroundStyle { public enum BackgroundStyle {

View File

@ -18,17 +18,13 @@ extension UIImage {
color.setFill() color.setFill()
context.fill(rect) context.fill(rect)
// text style // text
let paragraphStyle = NSMutableParagraphStyle() let text = PlaceholderBuilder().displayedText(size: size)
paragraphStyle.alignment = .center let attrs = PlaceholderBuilder().displayedTextAttributes()
let textRect = PlaceholderBuilder().textRect(imageSize: size)
let attrs = [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue", size: 10)!, NSAttributedString.Key.paragraphStyle: paragraphStyle]
// display string
let string = "\(Int(size.width))x\(Int(size.height))"
// draw text // draw text
string.draw(with: rect, options: .usesLineFragmentOrigin, attributes: attrs, context: nil) text.draw(with: textRect, options: .usesLineFragmentOrigin, attributes: attrs, context: nil)
} }
return image return image