0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[macApp] [Swift] About パネルのカスタマイズ

Posted at

About パネル

Mac アプリの多くは、メニューから「○○について」や「About ○○」をクリックすると、アプリの情報を示す「About パネル」が表示されます。


about0.png

このパネルを表示するためのコードは、たった 1行です。

NSApp.orderFrontStandardAboutPanel()
使用例
Button("About this app") {
    NSApp.orderFrontStandardAboutPanel()
}

標準で、アプリアイコンバンドル名バージョン情報 が表示されます。

カスタマイズ その1

orderFrontStandardAboutPanel(options:)の引数に オプションを指定する方法です。

let textAttributes: [NSAttributedString.Key : Any] = [
    .font : NSFont.systemFont(ofSize: 20.0),
    .strokeColor : NSColor.cyan,
    .strokeWidth : 3.0
]
let creditsText = NSAttributedString(string: "Copyright©2025", attributes: textAttributes)
let options: [NSApplication.AboutPanelOptionKey : Any] = [
    .applicationVersion : "1.2.3",
    .version : "abc",
    .applicationName : "Test App",
    .applicationIcon : NSImage(systemSymbolName: "figure", accessibilityDescription: nil)!,
    .credits : creditsText,
]
NSApp.orderFrontStandardAboutPanel(options: options)

credits.png

オプションの意味

・ ApplicationVersion
アプリケーションバージョン(String)
指定されなければ`CFBundleShortVersionString`の値
・ Version
アプリケーションのバージョン番号(String)
指定されなければ`CFBuildVersion`の値
・ ApplicationName
アプリケーション名(String)
指定されなければ`CFBundleName`の値
・ ApplicationIcon
アプリケーションのアイコン(NSImage)
指定されなければ、バンドルの`NSApplicationIcon`という名前の付いた画像。それもなければ、標準的なアプリケーションアイコン
・ Credits
属性付き文字列(NSAttributedString)
指定されなければ、バンドルの`Credits.rtf` / `Credits.html` ファイルの内容
・ Copyright
著作権情報(String)、
指定されなければ`NSHumanReadableCopyright`の値。それもなければ空

creditsにはNSAttributedStringを指定するので、リッチテキストのように指定できます。

対して、単純な 文字列で指定する場合は、creditsの代わりにCopyrightを指定します。

let options: [NSApplication.AboutPanelOptionKey : Any] = [
    .applicationVersion : "1.2.3",
    .version : "abc",
    .applicationName : "Test App",
    .applicationIcon : NSImage(systemSymbolName: "figure", accessibilityDescription: nil)!,
    .init(rawValue: "Copyright") : "Copyright©2025",
]
NSApp.orderFrontStandardAboutPanel(options: options)

copyright.png

.copyrightが未定義なのは、もしかしてCopyrightの使用は非推奨??

~ % swift repl
Welcome to Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4).
Type :help for assistance.
  1> import AppKit
  2> print(NSApplication.AboutPanelOptionKey.applicationVersion.rawValue)
ApplicationVersion
  3> print(NSApplication.AboutPanelOptionKey.version.rawValue) 
Version
  4> print(NSApplication.AboutPanelOptionKey.applicationName.rawValue) 
ApplicationName
  5> print(NSApplication.AboutPanelOptionKey.applicationIcon.rawValue) 
ApplicationIcon
  6> print(NSApplication.AboutPanelOptionKey.credits.rawValue) 
Credits
  7> print(NSApplication.AboutPanelOptionKey.copyright.rawValue) 
error: repl.swift:7:41: 
error: type 'NSApplication.AboutPanelOptionKey' has no member 'copyright'

カスタマイズ その2

orderFrontStandardAboutPanel()でオプションを指定せず、
プロジェクトにCredits.htmlCredits.rtfを作成して追加する方法があります。

・ 例えば、

次の html で URLリンク が追加されます。

Credits.html
<a style="font-family:-apple-system;" 
   href="https://nak435.com/">https://nak435.com/</a>

about1.png

・ リッチテキストでは、

rtf ファイルの内容がそのまま追加されます。

about2.png


詳しい説明は省きますが、カスタマイズその1その2を組み合わせた カスタマイズも指定できます。


参考



以上

0
1
0

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?