2

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.

  • What's the difference between width and height of the font? (like why would you need width instead of height) – aheze Apr 30 '21 at 03:53
  • 1
    @aheze I need the height because I'm creating a bitmap which has to be the same size as the font. (width and height) – AudioPlayer1234 Apr 30 '21 at 03:54
  • I don't know about Swift, but in C# you can use something like TextRenderer.MeasureText to determine the width of the text. Note, that other than fixed type fonts, most fonts have different widths for different letters. For example 'W' is much wider than 'i'. So can't do something like `fontWidth * str.Length`. That's why there is functions like MeasureText, because it gets the width for each unique character and uses that to calculate the total width. – B.O.B. Apr 30 '21 at 04:37
  • This thread probably has want you want for swift: https://stackoverflow.com/questions/3527494/how-to-calculate-uilabel-width-based-on-text-length – B.O.B. Apr 30 '21 at 04:39
  • @B.O.B. However I'm not looking to determine the length of the text. I'm trying to find the em-size of the font, without any characters. For example, if you create a new Font object and then call its `SizeInPoints` property in C#. – AudioPlayer1234 Apr 30 '21 at 05:59
  • @AudioPlayer1234 You mention width in post...It's easy to get the height, however for the width, unless you are using a fixed-width font, it's impossible to know the width without knowing each individual letter. For example, I took a screenshot of my previous msg, and the letter W in this website's font is 13 pixels width, and the letter i is 3 pixels width - an 10 pixel difference... However, when you do a code example in this site, it switches the font to fixed-width font - when I measure W & i, they are both 7 pixels wide. – B.O.B. May 01 '21 at 06:16
  • @AudioPlayer1234 didn't see your edits tell after my reply. Btw, I think you might be confusing some terms. 1 em = font size in points. So if the font size 16 pt, then 1 em = 16 pt. https://en.wikipedia.org/wiki/Em_(typography) – B.O.B. May 01 '21 at 06:21

0 Answers0