Consider the following as pseudo-code quickly made, not verified, etc.
Given NSFont , which represents a font that has monospace numbers as a function, the following method will produce another NSFont with the function selected:
- (NSFont *) newMonospaceNumbersFont:(NSFont *)font { CTFontDescriptorRef origDesc = CTFontCopyFontDescriptor((__bridge CTFontRef)font); CTFontDescriptorRef monoDesc = CTFontDescriptorCreateCopyWithFeature(origDesc, (__bridge CFNumberRef)@(kNumberSpacingType), (__bridge CFNumberRef)@(kMonospacedNumbersSelector)); CFRelease(origDesc); CTFontRef monoFont = CTFontCreateWithFontDescriptor(monoDesc, font.pointSize, NULL); CFRelease(monoDesc); return (__bridge_transfer NSFont *)monoFont; }
You can use this, say, to take the current font of a user interface element and convert it to one with monospace numbers.
NTN
Option for Swift
Assuming res is an NSTextField with a number to display:
let origDesc = CTFontCopyFontDescriptor(res.font!) let monoDesc = CTFontDescriptorCreateCopyWithFeature(origDesc, kNumberSpacingType, kMonospacedNumbersSelector) let monoFont = CTFontCreateWithFontDescriptor(monoDesc, res.font!.pointSize, nil) res.font = monoFont
CRD
source share