使用 NSAttributedString 绘图时在非视网膜 Mac 上呈现模糊字体
Blurry font rendering on non-retina Macs when using NSAttributedString drawing
我在 NSView
中使用 NSAttributedString
的 draw(at:)
函数在我的 window.[=27 中以自定义字体呈现一些文本=]
但是,当在非 Retina MacBook 上 运行 时(无论是在内部显示器上还是在外部 LCD 上),字体看起来都非常模糊并且 "too heavy"。
由于我能够在同一台机器上完美地重现 Sketch 中的预期结果,我假设这是我的代码的问题。
到目前为止,这是我的代码:
import Cocoa
class StepNameLabel: NSView {
// Im aware that this is not the intended way of loading Apple's system fonts
// However, this was the best way I could find to make sure that both Sketch
// and the app were using the exact same fonts.
var font = NSFont(name: "SF Pro Text Semibold", size: 22)
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let text = "Choose your Images"
let attributes: [NSAttributedString.Key: Any] = [
.font: font,
.foregroundColor: NSColor.white
]
print(font?.fontName)
let drawableString = NSAttributedString(string: text, attributes: attributes)
frame.size = drawableString.size()
drawableString.draw(at: NSPoint(x: 0, y: 0))
}
}
这是显示 Sketch 文件和应用程序 运行ning 在同一显示器上的区别的屏幕截图(左:Sketch 应用程序中的图形,右:以上代码的输出):
该应用程序的代码和 Sketch 图形均使用 Apple 的 "SF Pro Text" 字体,字体粗细为 "Semibold",大小为 22 个单位。
如能帮助找出问题所在,我们将不胜感激。
这可能是臭名昭著的 'half pixel' 问题。尝试:
drawableString.draw(at: NSPoint(x: 0.5, y: 0.5))
有一些关于此 here 的信息(在页面中搜索 'Points and Pixels')。
我在 NSView
中使用 NSAttributedString
的 draw(at:)
函数在我的 window.[=27 中以自定义字体呈现一些文本=]
但是,当在非 Retina MacBook 上 运行 时(无论是在内部显示器上还是在外部 LCD 上),字体看起来都非常模糊并且 "too heavy"。
由于我能够在同一台机器上完美地重现 Sketch 中的预期结果,我假设这是我的代码的问题。
到目前为止,这是我的代码:
import Cocoa
class StepNameLabel: NSView {
// Im aware that this is not the intended way of loading Apple's system fonts
// However, this was the best way I could find to make sure that both Sketch
// and the app were using the exact same fonts.
var font = NSFont(name: "SF Pro Text Semibold", size: 22)
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let text = "Choose your Images"
let attributes: [NSAttributedString.Key: Any] = [
.font: font,
.foregroundColor: NSColor.white
]
print(font?.fontName)
let drawableString = NSAttributedString(string: text, attributes: attributes)
frame.size = drawableString.size()
drawableString.draw(at: NSPoint(x: 0, y: 0))
}
}
这是显示 Sketch 文件和应用程序 运行ning 在同一显示器上的区别的屏幕截图(左:Sketch 应用程序中的图形,右:以上代码的输出):
该应用程序的代码和 Sketch 图形均使用 Apple 的 "SF Pro Text" 字体,字体粗细为 "Semibold",大小为 22 个单位。
如能帮助找出问题所在,我们将不胜感激。
这可能是臭名昭著的 'half pixel' 问题。尝试:
drawableString.draw(at: NSPoint(x: 0.5, y: 0.5))
有一些关于此 here 的信息(在页面中搜索 'Points and Pixels')。