将棋プログラミング

(将棋に関する)ソフトウェア開発のノウハウ等。

iOS の等幅フォント

iOS の通常のフォントは、半角空白が等幅でないが、iOS 13 から、半角空白を含め、等幅なフォントが使えるようになった。

1. monospacedDigitSystemFontOfSize: weight:

iOS 9.0 以降では、このフォントが使えるが、半角空白は等幅でない。
https://developer.apple.com/documentation/uikit/uifont/1619022-monospaceddigitsystemfontofsize?language=objc

The system font uses proportional spacing. 
If you want all characters to be fixed-width, use monospacedSystemFontOfSize:weight: instead.

2. monospacedSystemFontOfSize: weight:

iOS 13.0 以降では、このフォントが使え、半角空白も等幅である。
https://developer.apple.com/documentation/uikit/uifont/3042484-monospacedsystemfontofsize?language=objc

3.使用例

次が使用例である。

UITextView *textView;
if (@available(iOS 13.0, *)) {
    textView.font = [UIFont monospacedSystemFontOfSize: 13 weight: UIFontWeightRegular];
} else {
    textView.font = [UIFont monospacedDigitSystemFontOfSize: 13 weight: UIFontWeightRegular];
}