Fixed spacing numbers in AppKit - cocoa

I have an app with a timer. This timer is centered in the view. When macOS's default font was Helvetica Neue, this worked fine. It seems that the font's numbers are spaced evenly, so when the timer is running, everything stays in place.
Now with San Francisco as the default font, this is broken. Ever number seems to have a different width, causing the timer to flux in size continuously while running. Hard coding the font to Helvetica Neue fixes the issue, but I'd like to use the font that's default to the OS.
Is there some kind of hint I can set with NSTextField that would render the default system font with fixed width numbers like before? I seem to remember this being available, but I cannot find it. I don't want to use a fixed-width font, as it breaks the aesthetic. It seems like the stop watch app on iOS is doing this somehow.

Use this class method of NSFont
Swift
class func monospacedDigitSystemFont(ofSize fontSize: CGFloat, weight: CGFloat) -> NSFont
Objective-C
+ (NSFont *)monospacedDigitSystemFontOfSize:(CGFloat)fontSize weight:(CGFloat)weight;
Available macOS 10.11+

Related

How to get the vibrant effect for a NSTextField/NSSearchField like Safari's address bar in Big Sur

I have a Mac app with a search field in its toolbar (actually a NSTextField, but I tried NSSearchField as well).
My problem is that the text field background on Big Sur is just plain white, which makes it hard to recognize.
Safari's location bar on Big Sur has more contrast. I believe this is somehow achieved by using vibrancy (How to change background color of NSSearchField (like in Messages app in OS X)), but I cannot get the same effect.
I tried:
testTextField.appearance = NSAppearance(named: .vibrantLight)
various combinations of drawing/not drawing backgrounds
various background colors
wrapping my toolbar item into NSVisualEffectView, but that shows around the text field
Edit:
I should mention that I use roundedBezel border style. This alone seems to affect the background color (i.e. background is always white, no matter which color was set).

NSBox background color

Is there a system-defined NSColor for the background of an NSBox? Testing shows it to be RGB (226, 226, 226) but there does not seem to be anything like controlBackgroundColor for it to easily switch when in Dark Mode. I am using this color for the background of a custom NSView and don't really want to embed the view in an NSBox to get the effect I want.
From the AppKit Release Notes, the fillColor of a custom NSBox is set to controlBackgroundColor, windowBackgroundColor, or underPageBackgroundColor, but those aren't the same as the background for a default NSBox.
I asked this same question earlier in Apple's Developer Forums (also looking to provide a dark mode color), and aside from the usual boilerplate responses, the answer was that there is no system color name for it. The standard control and gray colors are a bit darker, so if you want to match the default you will need to roll your own.

Padding under font only on Mac

My horizontal nav bar and footer look perfect on PC, but when testing on Mac, the font is lifted about 30px above its position in the horizontal nav bar.
After trying every CSS reset and line-height adjustment, what finally worked on Mac made the font drop about 30px below its position in the horizontal nav bar on PC this time.
One Stackoverflow answer mentioned editing the glyph/baseline of a font.
I downloaded a free font editing program and noticed the characters inside of each respective square were lifted as if to have a 40px margin underneath, but I can't adjust the height of the baseline in the program.
Is there a free font manipulation program that will allow me to adjust the glyph/baseline of the font?
I appreciate your time in advance.
Try to generate your font via http://www.fontsquirrel.com/, and use the CSS for font-faces it gave you.
If the above does not work for you, try this:
Try to find out with JavaScript if the app is running on Mac. If so, load a new font-fix-mac.css file, where you will put margin-top:30px on all elements where the font is lifted above.

CALayer, NSTextView and scaling

In my app I want to provide text scaling in layer backed NSTextView like Apple's TextEdit. I use analogue of it's ScalingScrollView. Also I need to create some CALayer overlays on self.window.contentView. All is ok until I make [self.window.contentView setWantsLayer:YES].
Before [setWantsLayer:YES]
After [setWantsLayer:YES]
I haven't any ideas how to fix this problem.
I've been searching for the solution to the similar issue, too. Finally, I discovered that layer-backed views must be positioned on integral pixels and must not be positioned on subpixels.
E.g. if you dynamically calculate frame of layer-backed view
NSMakeRect((self.frame.size.width - 350)/2, (self.frame.size.height - 150)/2, 350, 150)
you may encounter non-integral values, so you should do something like
NSMakeRect(floor((self.frame.size.width - 350)/2), floor((self.frame.size.height - 150)/2), 350, 150)

Why does this text look "less bold" in OS X 10.5 compared to 10.7?

I built a very simple application with nothing but a single NSTextView in it in xcode / interface builder. I've done nothing to the text view other than change the font face to "Arial" and increase the font size. However, it looks a lot less bold on OS X 10.5 than it does on 10.7. What's going on here?
(10.5 in the top window, 10.7 in the bottom window)
I've tested with Helvetica and got the same results, so it's not something to do with this specific font.
If you enlarge that image enough, you'll see color fringing on the 10.7 sample. This indicates that the text was rendered with subpixel antialiasing, more specifically, using a RGB subpixel ordering. The 10.5 sample uses grayscale antialiasing. While it is generally a good idea to use subpixel rendering, it can look bad on low-resolution screens or CRTs, and it can't be used in a multi-monitor setup where there is more than one subpixel arrangement to contend with. This means that some users will have subpixel rendering enabled for their system, and some won't. Don't try to circumvent that setting in any way, either by overriding the system preference for font smoothing, or by pre-rendering the text into an image. Users are far more likely to notice the one app whose text looks wrong on their system than they are to notice the difference in rendering between two different machines.

Resources