I have this NSFont
object:
var font = NSFont(name: "Courier New", size: 5)
and I'm trying to get the em-size
of the font. You can use NSFont.pointSize
, but that returns the height of the NSFont
in points. I'm looking for the width of the font.
In C#
, where I am translating the code from, you can use
Font font = new Font("Courier New", 5f, FontStyle.Regular, GraphicsUnit.Point);
int widthInPoints = font.SizeInPoints; // Is there an equivalent of this line but in Swift?
Also, I would like to mention in my case the font will always be monospaced. (I think I'm going to use Courier New, as I don't see Lucida Console.)
I have tried using:
var widthInPoints = ("w" as NSString).size(withAttributes: [NSAttributedString.Key.font: font]).width
but I'm not sure that's correct, as I'm just merely looking for the width of the font without measuring a string. If it's correct however, then it's fine.
I've also tried using:
var width = font.boundingRectForFont.width
But once again, I'm not sure if this is correct. If it is correct, is the value returned by the boundingRectForFont
above in points or pixels? (The documentation is kinda unclear on that part)
NOTE:
I'm not trying to get the width of any string. I'm just trying to get the em-size
of the font. For example, you can call SizeInPoints
in C#
to get the original inputted size for the font. However on Swift
, it returns the height instead of width. How can I get the width instead?
Any help would be greatly appreciated.