D言語からObjective-CとCocoaAPIを使いたい
NSMutableAttributedStringで文字列を表示
フォントや色を変更した文字列を表示します。
NSMutableAttributedStringのSample

サンプルコード
Powered by Prism
attrstr/dub.json
{
"authors": ["hidhok"],
"description": "A D/Objective-C Cocoa application.",
"name": "attrstr",
"dflags": ["-L-framework", "-LCocoa"],
"sourceFiles": ["../cocoa/window",
"../cocoa/control",
"../cocoa/menus"]
}
attrstr/source/app.d
/**
* attrstr/source/app.d 0.0.4
*/
module attrstr.main;
import core.attribute : optional, selector;
import std.conv : to;
import std.stdio : printf;
import cocoa.window;
import cocoa.control;
import cocoa.menus;
string appName;
string versionNo = "0.0.4";
string helpMessage = "CocoaAPIを使用して、属性付文字列を\nD言語で表示しています。";
void main() {
auto delegate_ = AppDelegate.alloc.init;
NSApp.delegate_ = cast(void*) delegate_;
NSApp.activationPolicy = NSAppActivationPolicy.regular;
NSApp.run;
}
extern (Objective-C)
class DrawView : NSView {
override static DrawView alloc() @selector("alloc");
override DrawView init() @selector("init");
override DrawView init(NSRect) @selector("initWithFrame:");
override void drawRect(NSRect r) @selector("drawRect:") {
double w = r.size.width;
double h = r.size.height;
double er = w < h ? w / 400 : h / 400;
void print0(string str, CGFloat x, CGFloat y) {
str.toNs.drawAtPoint(NSMakePoint(x, y), null);
}
void print1(string str, CGFloat x, CGFloat y) {
NSFont f = NSFont.nameSize("YuMincho Demibold".toNs, 30 * er);
MuAttrStr(str, x, y, f, NSColor.green);
}
void print2(string str, CGFloat x, CGFloat y) {
NSFont f = NSFont.nameSize("Times New Roman".toNs, 30 * er);
MuAttrStr(str, x, y, f, NSColor.yellow);
}
void print3(string str, CGFloat x, CGFloat y) {
NSFont f = NSFont.nameSize("Helvetica Light Oblique".toNs, 30 * er);
MuAttrStr(str, x, y, f, NSColor.blue);
}
void print4(string str, CGFloat x, CGFloat y) {
NSFont f = NSFont.nameSize("Baskerville Bold Italic".toNs, 30 * er);
MuAttrStr(str, x, y, f, NSColor.magenta);
}
NSColor.white.setFill;
NSRectFill(r);
NSRect rectB = NSMakeRect(40 * er, 200 * er, 320 * er, 120 * er);
NSColor.blue.setFill;
NSRectFill(rectB);
NSRect rectY = NSMakeRect(40 * er, 80 * er, 320 * er, 120 * er);
NSColor.yellow.setFill;
NSRectFill(rectY);
print0("W:" ~ w.to!string, 50, 20);
print0("H:" ~ h.to!string, 100, 20);
string str1 = "ウクライナに平和を!";
string str2 = "Нет войне!";
string str3 = "Слава Україні";
print1(str1, 60 * er, 270 * er);
print2(str2, 125 * er, 220 * er);
print3(str2, 125 * er, 150 * er);
print4(str3, 100 * er, 100 * er);
}
}
void MuAttrStr(string s, CGFloat x, CGFloat y, NSFont f, NSColor c) {
NSMutableAttributedString mAttrStr;
mAttrStr = NSMutableAttributedString.alloc.init(s.toNs);
NSRange range0 = NSRange(0, mAttrStr.length);
mAttrStr.addAttribute("NSFont".toNs, f, range0);
mAttrStr.addAttribute("NSColor".toNs, c, range0);
mAttrStr.drawAtPoint(NSPoint(x, y));
}
extern (Objective-C)
class AppDelegate : NSObject {
override static AppDelegate alloc() @selector("alloc");
override typeof(this) init() @selector("init");
bool applicationShouldTerminateAfterLastWindowClosed(
NSNotification notification) @selector(
"applicationShouldTerminateAfterLastWindowClosed:") {
return true;
}
void applicationDidFinishLaunching(NSNotification notification)
@selector("applicationDidFinishLaunching:") {
buildWindow;
}
void aboutPanel() @selector("aboutPanel") {
dialog(appName, "Version " ~ versionNo);
}
void showHelp() @selector("showHelp") {
dialog("Help", helpMessage);
}
}
void buildWindow() {
NSWindow win;
NSRect w_rect = NSMakeRect(100, 550, 400, 400);
appName = NSProcessInfo.processInfo.processName.toDs;
NSApp.menu = miniMenu(appName);
immutable w_style = NSWindowStyleMask.titled |
NSWindowStyleMask.closable |
NSWindowStyleMask.resizable |
NSWindowStyleMask.miniaturizable;
win = NSWindow.alloc.init(w_rect, w_style, backing.buffered, false);
win.title = (appName ~ " (D言語テスト)").toNs;
DrawView dView = DrawView.alloc.init;
win.contentView = dView;
// win.bgColor = NSColor.white;
win.minSize = NSSize(200, 100);
ViewController vCtrler = ViewController.alloc.init;
vCtrler.view.addSubview = dView;
win.makeKeyAndOrderFront = null;
NSApp.activateIgnoringOtherApps = true;
}
extern (Objective-C)
class ViewController : NSViewController {
override static ViewController alloc() @selector("alloc");
override ViewController init() @selector("init");
override void loadView() @selector("loadView") {
printf("loadView\n");
}
override void viewDidLoad() @selector("viewDidLoad") {
printf("viewDidLoad\n");
}
}
cocoa/window.d
cocoa/control.d
cocoa/menus.d
- Log
- 2025-01-03 修正しました
- 2024-06-07 修正しました
- 2024-01-22 修正しました
- 2023-01-24 修正しました
- 2022-12-23 修正しました
- 2022-07-23 upload