MacOS:在 Swift 中向图像添加文本叠加层
MacOS: Add a text overlay to an image in Swift
我为 UIImage 编写了一个小扩展,它可以向图像添加文本:
extension UIImage {
func addTextToImage(textToAdd: String) -> UIImage {
let textColor = UIColor.white
let textFont = UIFont(name: "Snell Roundhand", size: 40)!
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(self.size, false, scale)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center
var textFontAttributes = [
NSAttributedString.Key.font: textFont,
NSAttributedString.Key.foregroundColor: textColor,
NSAttributedString.Key.paragraphStyle: paragraphStyle
] as [NSAttributedString.Key : Any]
self.draw(in: CGRect(origin: CGPoint.zero, size: self.size))
let textFrame = CGPoint(x: self.size.width/4, y: self.size.height/4)
let rect = CGRect(origin: textFrame, size: CGSize(width: self.size.width/2, height: self.size.height/2) )
text.draw(in: rect, withAttributes: textFontAttributes)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
结果应该是这样的:
现在我想在 MacOS 上对 NSImage 执行与 运行 相同的操作。
知道怎么做吗?
Whosebug 上的所有类似问题都非常陈旧,没有答案,或者在 Objective C
找到解决方案:
extension NSImage {
func addTextToImage(drawText text: String) -> NSImage {
let targetImage = NSImage(size: self.size, flipped: false) { (dstRect: CGRect) -> Bool in
self.draw(in: dstRect)
let textColor = NSColor.white
let textFont = NSFont(name: "Snell Roundhand", size: 36)! //Helvetica Bold
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center
var textFontAttributes = [
NSAttributedString.Key.font: textFont,
NSAttributedString.Key.foregroundColor: textColor,
] as [NSAttributedString.Key : Any]
let textOrigin = CGPoint(x: self.size.height/3, y: -self.size.width/4)
let rect = CGRect(origin: textOrigin, size: self.size)
text.draw(in: rect, withAttributes: textFontAttributes)
return true
}
return targetImage
}
}
我为 UIImage 编写了一个小扩展,它可以向图像添加文本:
extension UIImage {
func addTextToImage(textToAdd: String) -> UIImage {
let textColor = UIColor.white
let textFont = UIFont(name: "Snell Roundhand", size: 40)!
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(self.size, false, scale)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center
var textFontAttributes = [
NSAttributedString.Key.font: textFont,
NSAttributedString.Key.foregroundColor: textColor,
NSAttributedString.Key.paragraphStyle: paragraphStyle
] as [NSAttributedString.Key : Any]
self.draw(in: CGRect(origin: CGPoint.zero, size: self.size))
let textFrame = CGPoint(x: self.size.width/4, y: self.size.height/4)
let rect = CGRect(origin: textFrame, size: CGSize(width: self.size.width/2, height: self.size.height/2) )
text.draw(in: rect, withAttributes: textFontAttributes)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
结果应该是这样的:
现在我想在 MacOS 上对 NSImage 执行与 运行 相同的操作。
知道怎么做吗?
Whosebug 上的所有类似问题都非常陈旧,没有答案,或者在 Objective C
找到解决方案:
extension NSImage {
func addTextToImage(drawText text: String) -> NSImage {
let targetImage = NSImage(size: self.size, flipped: false) { (dstRect: CGRect) -> Bool in
self.draw(in: dstRect)
let textColor = NSColor.white
let textFont = NSFont(name: "Snell Roundhand", size: 36)! //Helvetica Bold
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center
var textFontAttributes = [
NSAttributedString.Key.font: textFont,
NSAttributedString.Key.foregroundColor: textColor,
] as [NSAttributedString.Key : Any]
let textOrigin = CGPoint(x: self.size.height/3, y: -self.size.width/4)
let rect = CGRect(origin: textOrigin, size: self.size)
text.draw(in: rect, withAttributes: textFontAttributes)
return true
}
return targetImage
}
}