在 NSImage 上以 90 度绘制文本
Draw text at 90 degree on NSImage
我有以下代码可以在 image.How 上绘制文本 我可以绘制倾斜 90 度的文本吗
let textStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
let textFontAttributes = [
NSAttributedStringKey.font: font,
NSAttributedStringKey.foregroundColor: textcolor,
NSAttributedStringKey.paragraphStyle: textStyle
]
text.draw(in: textRect, withAttributes: textFontAttributes)
我找到了一个很好的简单方法 - 但您将不得不稍微调整一下坐标。变换将原点移动到左下角
大部分答案来自较早的答案 - How to rotate text in NSView? (Mac OS, Cocoa, Swift),但需要进行一些更新。
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let font = NSFont.boldSystemFont(ofSize: 18)
let textStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
let textFontAttributes = [
NSAttributedString.Key.font: font,
NSAttributedString.Key.foregroundColor: NSColor.red,
NSAttributedString.Key.paragraphStyle: textStyle
]
let textRect = CGRect(x: 200, y: 200 , width: 200, height: 100)
"normal".draw(in: textRect, withAttributes: textFontAttributes)
let transf : NSAffineTransform = NSAffineTransform()
transf.rotate(byDegrees: 90)
transf.concat()
"200, -100".draw(at: NSMakePoint(200, -100), withAttributes:textFontAttributes)
"300, -50".draw(at: NSMakePoint(300, -50), withAttributes:textFontAttributes)
}
这是我的输出
这是具有可计算位置的方法演示
class DemoView: NSView {
override func draw(_ rect: NSRect) {
// .. your drawing code here
NSColor.red.set() // just for demo
rect.fill() // just for demo
if let context = NSGraphicsContext.current?.cgContext {
context.saveGState()
let text: NSString = "Hello World"
let attributes = [
NSAttributedString.Key.foregroundColor: NSColor.white,
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 24)
]
let size = text.size(withAttributes: attributes)
context.translateBy(x: size.height, y: (rect.height - size.width) / 2)
context.rotate(by: CGFloat.pi / 2)
text.draw(at: .zero, withAttributes: attributes)
context.restoreGState()
}
}
}
我有以下代码可以在 image.How 上绘制文本 我可以绘制倾斜 90 度的文本吗
let textStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
let textFontAttributes = [
NSAttributedStringKey.font: font,
NSAttributedStringKey.foregroundColor: textcolor,
NSAttributedStringKey.paragraphStyle: textStyle
]
text.draw(in: textRect, withAttributes: textFontAttributes)
我找到了一个很好的简单方法 - 但您将不得不稍微调整一下坐标。变换将原点移动到左下角
大部分答案来自较早的答案 - How to rotate text in NSView? (Mac OS, Cocoa, Swift),但需要进行一些更新。
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let font = NSFont.boldSystemFont(ofSize: 18)
let textStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
let textFontAttributes = [
NSAttributedString.Key.font: font,
NSAttributedString.Key.foregroundColor: NSColor.red,
NSAttributedString.Key.paragraphStyle: textStyle
]
let textRect = CGRect(x: 200, y: 200 , width: 200, height: 100)
"normal".draw(in: textRect, withAttributes: textFontAttributes)
let transf : NSAffineTransform = NSAffineTransform()
transf.rotate(byDegrees: 90)
transf.concat()
"200, -100".draw(at: NSMakePoint(200, -100), withAttributes:textFontAttributes)
"300, -50".draw(at: NSMakePoint(300, -50), withAttributes:textFontAttributes)
}
这是我的输出
这是具有可计算位置的方法演示
class DemoView: NSView {
override func draw(_ rect: NSRect) {
// .. your drawing code here
NSColor.red.set() // just for demo
rect.fill() // just for demo
if let context = NSGraphicsContext.current?.cgContext {
context.saveGState()
let text: NSString = "Hello World"
let attributes = [
NSAttributedString.Key.foregroundColor: NSColor.white,
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 24)
]
let size = text.size(withAttributes: attributes)
context.translateBy(x: size.height, y: (rect.height - size.width) / 2)
context.rotate(by: CGFloat.pi / 2)
text.draw(at: .zero, withAttributes: attributes)
context.restoreGState()
}
}
}