LoginSignup
1

More than 1 year has passed since last update.

[Delphi][小ネタ] Windows / macOS / iOS / Android で等幅フォントを取得する

Last updated at Posted at 2022-12-21

等幅フォントを取得する

コードはもちろんのこと情報を表示するときとかに等幅フォントが必要になる事があります。
各プラットフォーム毎にフォント名が異なるのでめんどい!のと毎回調べてるので、メソッドにまとめてみました。

コードは以下の通り。

(*
 * Font Utility
 *
 * PLATFORMS
 *   Windows / macOS / iOS / Android
 *
 * LICENSE
 *   Copyright (c) 2022 HOSOKAWA Jun
 *   Released under the MIT license
 *   http://opensource.org/licenses/mit-license.php
 *
 * HISTROY
 *   2022/12/19 Version 1.0.0  First Release
 *
 * Programmed by HOSOKAWA Jun (twitter: @pik)
 *)

unit PK.Utils.Font;

interface

type
  TFontUtils = record
  public
    class function GetMonospaceFont: String; static;
  end;

implementation

{ TFontUtils }

class function TFontUtils.GetMonospaceFont: String;
begin
  {$IFDEF MSWINDOWS}
    Result := 'MS Gothic'; // 英字だけで良いなら Consolas
  {$ENDIF}
  {$IFDEF OSX}
    Result := 'Osaka-mono'; // 英字だけで良いなら Menlo
  {$ENDIF}
  {$IFDEF iOS}
    Result := 'Courier'
  {$ENDIF}
  {$IFDEF Android}
    Result := 'monospace';
  {$ENDIF}
end;

end.

使い方

こんな感じでいけます。
便利~

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.TextSettings.Font.Family := TFontUtils.GetMonospaceFont;
end;

補足

iOS は↓こんな感じでシステムのフォントを等幅フォントとして取得できるらしいです。

Swiftで記述
UIFont.monospacedDigitSystemFont(ofSize: 16, weight: .medium)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1