Render text rect on macOS
This commit is contained in:
parent
1a9df20313
commit
80bb42fa79
|
@ -6,14 +6,26 @@
|
|||
// Copyright © 2018 Shao Ping Lee. All rights reserved.
|
||||
//
|
||||
|
||||
#if canImport(AppKit)
|
||||
import AppKit
|
||||
#if canImport(Cocoa)
|
||||
import Cocoa
|
||||
|
||||
extension NSImage {
|
||||
static func image(withColor color: NSColor, size: CGSize) -> NSImage? {
|
||||
let image = NSImage(size: size)
|
||||
let rect = CGRect(origin: .zero, size: size)
|
||||
|
||||
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()
|
||||
return image
|
||||
}
|
||||
|
|
|
@ -10,10 +10,12 @@
|
|||
import AppKit
|
||||
public typealias Color = NSColor
|
||||
public typealias Image = NSImage
|
||||
public typealias Font = NSFont
|
||||
#else
|
||||
import UIKit
|
||||
public typealias Color = UIColor
|
||||
public typealias Image = UIImage
|
||||
public typealias Font = UIFont
|
||||
#endif
|
||||
|
||||
public struct Placeholder {
|
||||
|
@ -35,24 +37,34 @@ public struct PlaceholderBuilder {
|
|||
return nil
|
||||
}
|
||||
|
||||
public init() {}
|
||||
}
|
||||
// not platform specific
|
||||
public func displayedTextAttributes() -> [NSAttributedString.Key: NSObject] {
|
||||
let paragraphStyle = NSMutableParagraphStyle()
|
||||
paragraphStyle.alignment = .center
|
||||
|
||||
public struct PlaceholderSettings {
|
||||
let size: CGSize
|
||||
let background: BackgroundStyle
|
||||
let showDimensionAsText: Bool
|
||||
let convertDimensionToAspectRatio: Bool
|
||||
let attrs = [NSAttributedString.Key.font: Font(name: "HelveticaNeue", size: 10)!, NSAttributedString.Key.paragraphStyle: paragraphStyle]
|
||||
|
||||
public init(size: CGSize,
|
||||
background: BackgroundStyle,
|
||||
showDimensionAsText: Bool,
|
||||
convertDimensionToAspectRatio: Bool) {
|
||||
self.size = size
|
||||
self.background = background
|
||||
self.showDimensionAsText = showDimensionAsText
|
||||
self.convertDimensionToAspectRatio = convertDimensionToAspectRatio
|
||||
return attrs
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
@ -18,17 +18,13 @@ extension UIImage {
|
|||
color.setFill()
|
||||
context.fill(rect)
|
||||
|
||||
// text style
|
||||
let paragraphStyle = NSMutableParagraphStyle()
|
||||
paragraphStyle.alignment = .center
|
||||
|
||||
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))"
|
||||
// text
|
||||
let text = PlaceholderBuilder().displayedText(size: size)
|
||||
let attrs = PlaceholderBuilder().displayedTextAttributes()
|
||||
let textRect = PlaceholderBuilder().textRect(imageSize: size)
|
||||
|
||||
// draw text
|
||||
string.draw(with: rect, options: .usesLineFragmentOrigin, attributes: attrs, context: nil)
|
||||
text.draw(with: textRect, options: .usesLineFragmentOrigin, attributes: attrs, context: nil)
|
||||
}
|
||||
|
||||
return image
|
||||
|
|
Loading…
Reference in New Issue