Slide 75
Slide 75 text
static func generate(text: String, size: CGFloat = 100,
imageSize: NSSize = NSSize(width: 1920, height: 1080)) -> CIImage? {
let font = NSFont(name: "HiraginoSans-W9", size: size) ?? NSFont.systemFont(ofSize: size)
let image = NSImage(size: imageSize, flipped: false) { (rect) -> Bool in
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
paragraphStyle.lineBreakMode = .byCharWrapping
let numberOfLines: CGFloat = CGFloat(text.split(separator: "\n").count)
let rectangle = NSRect(x: 0, y: imageSize.height - font.lineHeight() * numberOfLines,
width: imageSize.width, height: font.lineHeight() * numberOfLines)
let textAttributes = [
.strokeColor: NSColor.black,
.foregroundColor: NSColor.white,
.strokeWidth: -1,
.font: font,
.paragraphStyle: paragraphStyle
] as [NSAttributedString.Key : Any]
(text as NSString).draw(in: rectangle, withAttributes: textAttributes)
return true
}
return image.ciImage
}
6UJMJUZTXJGU